Alex Rivera | Logout

How to handle exceptions from a BackgroundWorker thread?

Asked 2010-02-20T16:05:19.210
9

In a WPF app I have a sheduled database access task, periodically run by a timer and this task have been executed in a BackgroundWorker thread.

When connection attempt failed I raise an exception by try_catch construction and I want to update a Status Bar text in a UI thread.

Is there some prebuild event construction in a BackgroundWorker for implementing this, something like DoWorkEventHandler or RunWorkerCompletedEventHandler, which can be used for this? If not, how to do it better?

Edited (added):

If I want to handle the exception inside RunWorkerCompletedEventHandler, using e.Error parameter, it doesn't work. In case I leave exception unhandled in the BackgroundWorker thread, application hangs on and debugger points to the string of code which is excuted inside BackgroundWorker thread, saying that: Exception was unhandled by user code.

So, in this case, thread doesn't just stop, signalling to RunWorkerCompletedEventHandler that it stopped with error, but the whole application stop working.

Edit
Report

1 Answer

3
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
     Exception exceptionThrowDuringDoWorkEventHandler = e.Error;
}
answered 2010-02-20T16:13:49.200

Your Answer