The .NET BackgroundWorker class provides an easy way to perform long-running tasks without blocking the user interface thread. Unlike queuing tasks to the ThreadPool directly exceptions that happen on the background thread are not fatal to the application. Instead, they are marshaled back to the foreground thread and provided as part of the completion event.
Without specific handling, exceptions will silently not be reported or logged. To report them to the user through the Error Manager (and ensure they get recorded) you need to forward the exception like in the sample below
Reporting exceptions in a background Worker |
Copy Code
|
---|---|
//in the run completion notification event you need to report the error private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { //we failed - an exception was thrown on the background DO Work event. Log.ReportException(e.Error, LogCategory, true, true); } } |