Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 4.42 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. RunWorkerCompleted exception not reported
  2. public static void Execute()
  3. {
  4.     while (_isActive)
  5.     {
  6.         SemaphoreFile semaphoreFile;
  7.         lock (ThreadLock)
  8.         {
  9.             int threadIndex;
  10.             bool hasFreeThread = TryGetFreeThread(out threadIndex);
  11.             bool hasSemaphore = SemaphoreQueue.HasNext;
  12.             if (hasFreeThread && hasSemaphore)
  13.             {
  14.                 semaphoreFile = SemaphoreQueue.Next;
  15.                 _threads[threadIndex].DoWork += delegate(object sender, DoWorkEventArgs args)
  16.                 {
  17.                     try
  18.                     {
  19.                         BackgroundWorker bw = (BackgroundWorker)sender;
  20.                         if (bw.CancellationPending)
  21.                             args.Cancel = true;
  22.                         args.Result = semaphoreFile.GetResults(bw);
  23.                         if (bw.CancellationPending)
  24.                             args.Cancel = true;
  25.                     }
  26.                     catch (Exception e)
  27.                     {
  28.                         Writelog.Write("An error occured processing file " + semaphoreFile + ":" + e.Message);
  29.                         Writelog.WriteException(e);
  30.                     }
  31.                 }
  32.                 ;
  33.                 _threads[threadIndex].RunWorkerCompleted += ThreadRunWorkerCompleted;
  34.                 Debug.Print("Starting Thread @ index " + threadIndex);
  35.                 _threads[threadIndex].RunWorkerAsync();
  36.             }
  37.         }
  38.     }
  39. }
  40.        
  41. private static void ThreadRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  42. {
  43.     if (e.Error != null)
  44.     {
  45.         if (e.Error.GetType() == typeof(SemaphoreFileException))
  46.         {
  47.             SemaphoreFileException semaphoreFileException = (SemaphoreFileException)e.Error;
  48.             semaphoreFileException.SemaphoreFile.MarkAsProblematic(e.Error);
  49.             semaphoreFileException.SemaphoreFile.Dispose();
  50.         }
  51.         else
  52.         {
  53.             if (!(e.Error.GetType() == typeof(SemaphoreResultFileException)))
  54.             {
  55.                 throw e.Error;
  56.             }
  57.             SemaphoreResultFileException semaphoreResultFileException = (SemaphoreResultFileException)e.Error;
  58.             semaphoreResultFileException.SemaphoreFile.MarkAsProblematic(e.Error);
  59.             semaphoreResultFileException.SemaphoreFile.Dispose();
  60.         }
  61.     }
  62.     else
  63.     {
  64.         if (!e.Cancelled)
  65.         {
  66.             if (e.Result == null)
  67.             {
  68.                 throw new NotImplementedException("No error or results");
  69.             }
  70.             if (e.Result.GetType() == typeof(SemaphoreFile))
  71.             {
  72.                 SemaphoreFile semaphoreFile = (SemaphoreFile)e.Result;
  73.                 semaphoreFile.Dispose();
  74.             }
  75.         }
  76.     }
  77. }
  78.        
  79. public SemaphoreFile GetResults(BackgroundWorker backgroundWorker)
  80. {
  81.     if (!File.Exists(_fileInfo.FullName))
  82.         throw new SemaphoreFileException(this, string.Format("Semaphore {0} is missing", _fileInfo.FullName));
  83.     if (!Parse())
  84.         throw new SemaphoreFileException(this, string.Format("Semaphore {0} failed to parse correctly.", _fileInfo.FullName));
  85.     if (!_resultFiles.Any())
  86.         throw new SemaphoreFileException(this, string.Format("Semaphore {0} contains no Results.", _fileInfo.FullName));
  87.  
  88.     List<Exception> resultFileExceptions = new List<Exception>();
  89.     foreach (ResultFile resultFile in _resultFiles)
  90.     {
  91.         if (backgroundWorker.CancellationPending)
  92.         {
  93.             IsFinished = false;
  94.             return this;
  95.         }
  96.         try
  97.         {
  98.             resultFile.Process();
  99.             string line = resultFile.ToString();
  100.             if (_lines.Contains(resultFile.ToString()))
  101.             {
  102.                 _lines.Remove(line);
  103.                 if (!_lines.Contains("Lines done in last run:"))
  104.                 {
  105.                     _lines.Add("Run @ " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
  106.                     _lines.Add("Lines done in last run:");
  107.                 }
  108.                 _lines.Add(resultFile.ToString());
  109.             }
  110.         }
  111.         catch (Exception exception)
  112.         {
  113.             resultFileExceptions.Add(exception);
  114.         }
  115.     }
  116.     if (resultFileExceptions.Count > 0)
  117.     {
  118.         _fileInfo = MoveFile(SemaphoreSubFoldersEnum.Problematic);
  119.         throw new SemaphoreResultFileException(this, resultFileExceptions, string.Format("Results in Semaphore {0} contained errors:", _fileInfo.FullName));
  120.     }
  121.     return this;
  122. }