- RunWorkerCompleted exception not reported
- public static void Execute()
- {
- while (_isActive)
- {
- SemaphoreFile semaphoreFile;
- lock (ThreadLock)
- {
- int threadIndex;
- bool hasFreeThread = TryGetFreeThread(out threadIndex);
- bool hasSemaphore = SemaphoreQueue.HasNext;
- if (hasFreeThread && hasSemaphore)
- {
- semaphoreFile = SemaphoreQueue.Next;
- _threads[threadIndex].DoWork += delegate(object sender, DoWorkEventArgs args)
- {
- try
- {
- BackgroundWorker bw = (BackgroundWorker)sender;
- if (bw.CancellationPending)
- args.Cancel = true;
- args.Result = semaphoreFile.GetResults(bw);
- if (bw.CancellationPending)
- args.Cancel = true;
- }
- catch (Exception e)
- {
- Writelog.Write("An error occured processing file " + semaphoreFile + ":" + e.Message);
- Writelog.WriteException(e);
- }
- }
- ;
- _threads[threadIndex].RunWorkerCompleted += ThreadRunWorkerCompleted;
- Debug.Print("Starting Thread @ index " + threadIndex);
- _threads[threadIndex].RunWorkerAsync();
- }
- }
- }
- }
- private static void ThreadRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- if (e.Error != null)
- {
- if (e.Error.GetType() == typeof(SemaphoreFileException))
- {
- SemaphoreFileException semaphoreFileException = (SemaphoreFileException)e.Error;
- semaphoreFileException.SemaphoreFile.MarkAsProblematic(e.Error);
- semaphoreFileException.SemaphoreFile.Dispose();
- }
- else
- {
- if (!(e.Error.GetType() == typeof(SemaphoreResultFileException)))
- {
- throw e.Error;
- }
- SemaphoreResultFileException semaphoreResultFileException = (SemaphoreResultFileException)e.Error;
- semaphoreResultFileException.SemaphoreFile.MarkAsProblematic(e.Error);
- semaphoreResultFileException.SemaphoreFile.Dispose();
- }
- }
- else
- {
- if (!e.Cancelled)
- {
- if (e.Result == null)
- {
- throw new NotImplementedException("No error or results");
- }
- if (e.Result.GetType() == typeof(SemaphoreFile))
- {
- SemaphoreFile semaphoreFile = (SemaphoreFile)e.Result;
- semaphoreFile.Dispose();
- }
- }
- }
- }
- public SemaphoreFile GetResults(BackgroundWorker backgroundWorker)
- {
- if (!File.Exists(_fileInfo.FullName))
- throw new SemaphoreFileException(this, string.Format("Semaphore {0} is missing", _fileInfo.FullName));
- if (!Parse())
- throw new SemaphoreFileException(this, string.Format("Semaphore {0} failed to parse correctly.", _fileInfo.FullName));
- if (!_resultFiles.Any())
- throw new SemaphoreFileException(this, string.Format("Semaphore {0} contains no Results.", _fileInfo.FullName));
- List<Exception> resultFileExceptions = new List<Exception>();
- foreach (ResultFile resultFile in _resultFiles)
- {
- if (backgroundWorker.CancellationPending)
- {
- IsFinished = false;
- return this;
- }
- try
- {
- resultFile.Process();
- string line = resultFile.ToString();
- if (_lines.Contains(resultFile.ToString()))
- {
- _lines.Remove(line);
- if (!_lines.Contains("Lines done in last run:"))
- {
- _lines.Add("Run @ " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
- _lines.Add("Lines done in last run:");
- }
- _lines.Add(resultFile.ToString());
- }
- }
- catch (Exception exception)
- {
- resultFileExceptions.Add(exception);
- }
- }
- if (resultFileExceptions.Count > 0)
- {
- _fileInfo = MoveFile(SemaphoreSubFoldersEnum.Problematic);
- throw new SemaphoreResultFileException(this, resultFileExceptions, string.Format("Results in Semaphore {0} contained errors:", _fileInfo.FullName));
- }
- return this;
- }