2013-02-10 3 views
0

임 Parallel.For의 날짜 시간 깎다하려고 : 코드의Parallel.For 오류

부분 :

 public void LoadLogFile(String fileName) { 
     //Thread.CurrentThread.Priority = ThreadPriority.Lowest; 

     String currentFile = ""; 
     if (fileName.Contains("Compass")) { 
      currentFile = "Compass"; 
      CompassLogLoadCompleted = false; 
      compassLogCollection.Clear(); 
      compassLogCollection.AsParallel(); 
     } else if (fileName.Contains("")) { 
      currentFile = "CoreService"; 
      CoreServiceLogLoadCompleted = false; 
      coreServiceLogCollection.Clear(); 
      ; 
      compassLogCollection.AsParallel(); 
     } else { 
      Console.Out.WriteLine("Wrong File"); 
     } 

     if (fileName.Contains("CoreService") || 
      fileName.Contains("Compass")) { 
      int numberOfSingleLineLog = 0; 
      int numberOfmultipleLineLog = 0; 
      String[] lines = new string[] {}; 

      String temp = ""; 
      string[] parts; 
      DateTime dateTime = new DateTime(); 
      LoggingLvl loggingLvl = new LoggingLvl(); 
      LoggingLvl.ELoggingLvl eLoggingLvl = new LoggingLvl.ELoggingLvl(); 
      int id = 0; 
      char[] delimiters = new[] {' '}; 
      string threadId = ""; 
      string loggingMessage; 
      string loggingMessage2 = ""; 
      //string dateAndTimestamp = ""; 
      int ff = 0; 

      // Read the File and add it to lines string 
      try { 
       swCompass.Start(); 
       lines = File.ReadAllLines(fileName); 
       swCompass.Stop(); 
      } catch (Exception e) { 
       CompassLogLoadCompleted = true; 
       CoreServiceLogLoadCompleted = true; 
       Console.WriteLine("The file could not be read:"); 
       Console.WriteLine(e.Message); 
      } 

      swCompass.Reset(); 
      swCompass.Start(); 
      // Adding the objects to the collections 

      //compassLogCollection.EnableNotify = false; 

      Parallel.For(0, lines.Count(), j => { 
               //for (int i = 0; i < lines.Count(); i++) { 
               string dateAndTimestamp = ""; 
               if (!CompassLogLoadCompleted || !CoreServiceLogLoadCompleted) { 
                try { 
                 if (SingleLined(ref lines, j)) { 
                  parts = lines[j].Split(delimiters, 
                        StringSplitOptions. 
                         RemoveEmptyEntries); 
                  numberOfSingleLineLog++; 
                  foreach (string t in parts) { 
                   switch (ff) { 
                    case 0: 
                     dateAndTimestamp = t; 
                     break; 
                    case 1: 
                     dateAndTimestamp += " " + t.Replace(",", "."); 
                     dateTime = DateTime.Parse(dateAndTimestamp); 
                     //dateTime = new DateTime(); 
                     dateAndTimestamp = ""; 
                     break; 
                    case 2: 
                     eLoggingLvl = loggingLvl.ParseLoggingLvl(t); 
                     break; 
                    case 3: 
                     threadId = t; 
                     break; 

                    default: 
                     temp += t; 
                     break; 
                   } 

                   ff++; 
                  } 

                  loggingMessage = temp; 

                  temp = ""; 
                  ff = 0; 
                  id++; 
                  loggingLvl = new LoggingLvl(eLoggingLvl); 

                  if (fileName.Contains("Compass")) { 
                   //CompassLogLoadPercent = ((double) numberOfSingleLineLog/lines.Count())*100; 

                   CompassLogData cLD = 
                    new CompassLogData(
                     (numberOfSingleLineLog + 
                     numberOfmultipleLineLog), 
                     dateTime, 
                     loggingLvl, threadId, 
                     loggingMessage); 

                   //await addRoCompassLogCollectionAsync(cLD); 
                   compassLogCollection.Add(cLD); 
                  } else if (fileName.Contains("CoreService")) { 
                   CoreServiceLogData cSLD = 
                    new CoreServiceLogData(
                     (numberOfSingleLineLog + 
                     numberOfmultipleLineLog), 
                     dateTime, 
                     loggingLvl, 
                     threadId, 
                     loggingMessage); 
                   //await addRoCoreServiceCollectionAsync(cSLD); 
                   coreServiceLogCollection.Add(cSLD); 
                  } else { 
                   Console.Out.WriteLine("File Not recognizable "); 
                  } 

                  //Console.Out.WriteLine(loggingMessage); 
                  //loggingMessage = ""; 
                 } else { 
                  loggingMessage2 += lines[j]; 
                  loggingMessage2 += "\n"; 
                  //parts[i] += lines[i]; 
                  //parts[i] += "\n"; 

                  if (NextLineIsANumber(ref lines, j)) { 
                   numberOfmultipleLineLog++; 
                   //Console.Out.WriteLine(loggingMessage2); 
                   parts = loggingMessage2.Split(delimiters, 
                          StringSplitOptions. 
                           RemoveEmptyEntries); 
                   foreach (string t in parts) { 
                    switch (ff) { 
                     case 0: 
                      dateAndTimestamp = t; 
                      break; 
                     case 1: 
                      dateAndTimestamp += " " + 
                           t.Replace(",", "."); 
                      //dateTime = DateTime.Parse(dateAndTimestamp); 
                      dateTime = new DateTime(); 
                      dateAndTimestamp = ""; 
                      break; 
                     case 2: 
                      eLoggingLvl = 
                       loggingLvl.ParseLoggingLvl(t); 
                      break; 
                     case 3: 
                      threadId = t; 
                      break; 

                     default: 
                      temp += t; 
                      break; 
                    } 

                    ff++; 
                   } 

                   loggingMessage = temp; 

                   temp = ""; 
                   ff = 0; 
                   id++; 
                   loggingLvl = new LoggingLvl(eLoggingLvl); 

                   if (fileName.Contains("Compass")) { 
                    CompassLogData cLD = 
                     new CompassLogData(
                      (numberOfSingleLineLog + 
                      numberOfmultipleLineLog), 
                      dateTime, 
                      loggingLvl, threadId, 
                      loggingMessage); 

                    //await addRoCompassLogCollectionAsync(cLD); 
                    compassLogCollection.Add(cLD); 
                   } else if (fileName.Contains("CoreService")) { 
                    CoreServiceLogData cSLD = 
                     new CoreServiceLogData(
                      (numberOfSingleLineLog + 
                      numberOfmultipleLineLog), 
                      dateTime, 
                      loggingLvl, 
                      threadId, 
                      loggingMessage); 

                    //await addRoCoreServiceCollectionAsync(cSLD); 
                    coreServiceLogCollection.Add(cSLD); 
                   } else { 
                    Console.Out.WriteLine("File Not recognizable "); 
                   } 

                   loggingMessage2 = ""; 
                  } 
                 } 
                } catch (Exception e) { 
                 Console.Out.WriteLine("Shit Happens"); 
                 Console.Out.WriteLine(e.StackTrace); 
                } 

                if (currentFile == "Compass") { 
                 //CompassLogLoadPercent = 
                 // ((double) 
                 //  i 
                 //  /lines.Count())*100; 

                 CompassLogLoadPercent = ((double) 
                       j 
                       /lines.Count())*100; 
                } else if (currentFile == "CoreService") { 
                 CoreServiceLogLoadPercent = 
                  ((double) 
                  j 
                  /lines.Count())*100; 
                } 
               } 
              }); 
      //} 
      //compassLogCollection.EnableNotify = true; 
      //compassLogCollection.notifyAll(); 

      if (currentFile == "Compass") { 
       Console.Out.WriteLine("Compass TIME: " + swCompass.Elapsed); 
      } else { 
       Console.Out.WriteLine("CoreSevice TIME: " + swCompass.Elapsed); 
      } 

      if (currentFile == "Compass") { 
       CompassLogLoadCompleted = true; 
       Console.Out.WriteLine("Compass LOADING DONE"); 
      } else if (currentFile == "CoreService") { 
       CoreServiceLogLoadCompleted = true; 
       Console.Out.WriteLine("CoreService LOADING DONE"); 
      } 
      //CoreServiceLogLoadCompleted = true; 
      Console.Out.WriteLine("numberOfSingleLineLog: " + 
            numberOfSingleLineLog); 
      Console.Out.WriteLine("numberOfmultipleLineLog: " + 
            numberOfmultipleLineLog); 
      Console.Out.WriteLine("numberOfLogs: " + 
            (numberOfSingleLineLog + 
            numberOfmultipleLineLog)); 
      Console.Out.WriteLine(""); 
      //} 
     } 
    } 

그러나 나는 다음과 같은 예외가 얻을 :

at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) 
    at System.DateTime.Parse(String s) 
    at LogViewerV1.LogSession.<>c__DisplayClass3.<LoadLogFile>b__0(Int32 i) in     c:\Users\Reza\Documents\Visual Studio 2012\Projects\Pallas informatik\LogViewerV1\LogViewerV1\src\LogSession.cs:line 169 
    A first chance exception of type 'System.FormatException' occurred in mscorlib.d 

하는 경우를 나는이 예외를 모든 일을 잘 모르겠 루프 일반에 대한 실행합니다. 모든 아이디어 해결 방법?

+0

을 정의한다 : 당신이 무엇을해야

명시 적으로 사용하는 문화를 지정하는 것입니다? 그리고 '임시'? 'Parallel.For'가 작동하려면,'Parallel.For' 밖의 임의의 단일 상태에 따라 아마 쓰러 질 것입니다. – Joe

+0

이것은 TPL과 어떤 관련이 있습니까? 메시지가 알려주므로 입력 문자열이 유효하지 않습니다. – usr

+0

for 루프에서 실행하면 모든 것이 잘됩니다 ... – persianLife

답변

0

FormatException은 입력 된 DateTime이 예상 된 형식이 아님을 나타냅니다. DateTypeStyle을 지정할 수있는 DateTime.Parse의 오버로드를 사용해야합니다.

내가 문제가 어떻게 든 메인 쓰레드의 문화를 설정하는 것이 생각