Parallel.For error

时间:2013-02-10 17:45:35

标签: c# datetime task-parallel-library parallel.foreach

我试图在Parallel.For中削减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:\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

如果我在普通的for循环中运行它,我不会得到任何异常,一切正常。 任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

您应该做的是明确指定要使用的文化:

DateTime.Parse(dateAndTimestamp, theCorrectCulture)