并行.错误



我正试图在并行中削减DateTime。对于:

代码的一部分:

        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:UsersRezaDocumentsVisual Studio 2012ProjectsPallas informatikLogViewerV1LogViewerV1srcLogSession.cs:line 169
   A first chance exception of type 'System.FormatException' occurred in mscorlib.d

如果我在一个普通的for循环中运行这个,我不会得到任何异常,一切都很好。知道怎么解决这个问题吗?

FormatException表示输入DateTime不是预期格式。您应该使用DateTime.Parse的重载,它允许您指定DateTypeStyle。

请参阅http://msdn.microsoft.com/en-us/library/system.datetime.parse.aspx#Parse1_Example

我认为问题在于您以某种方式设置了主线程的区域性。但是区域性不会复制到任何其他线程,因此运行(部分)Parallel.For()循环的后台线程具有不同的区域性。

您应该做的是明确指定要使用的区域性:

DateTime.Parse(dateAndTimestamp, theCorrectCulture)

相关内容

  • 没有找到相关文章

最新更新