0

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 ループで実行すると、例外は発生せず、すべて正常に動作します。これを修正する方法はありますか?

4

2 に答える 2

0

FormatExceptionは、入力DateTimeが予期された形式ではないことを示します。DateTypeStyleを指定できるDateTime.Parseのオーバーロードを使用する必要があります。

http://msdn.microsoft.com/en-us/library/system.datetime.parse.aspx#Parse1_Exampleを参照してください

于 2013-02-10T17:53:44.327 に答える
0

問題は、メインスレッドの文化を何らかの形で設定していることだと思います。ただし、カルチャは他のスレッドにコピーされないため、Parallel.For()ループ (の一部) を実行するバックグラウンド スレッドは異なるカルチャを持ちます。

あなたがすべきことは、使用するカルチャを明示的に指定することです:

DateTime.Parse(dateAndTimestamp, theCorrectCulture)
于 2013-02-10T18:38:07.673 に答える