Reputation: 35
I m using the folowing code to handle exception from BackgroundWorker. The problem is that the exception is not propaged to the AppDomain.CurrentDomain.UnhandledException.
App.xaml.cs:
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
I have a pool of BackgroundWorker in the main App. Each BackgroundWorker represent a progress bar. The implementation of the DoWork is a follow:
private void BackgroundWorker_EditIni(object sender, DoWorkEventArgs e)
{
try
{
throw new Exception("Test");
}
catch (ApplicationException ex)
{
FailureMessage = ex.Message;
e.Cancel = true;
}
catch (Exception)
{
FailureMessage = DialogMessage.InternalError;
e.Cancel = true;
throw;
}
}
Upvotes: 2
Views: 240
Reputation: 8992
BackgroundWorker
will handle any unhandled exception in the DoWork
event handler. So if you just let the error get thrown (as you already are), then you should be able to get it in the RunWorkerCompleted
event by checking RunWorkerCompletedEventArgs.Error
property (which will be null if no exception was thrown.
I'm not entirely sure why you want this error to go to the AppDomain.UnhandledException
event handler, but if for some reason your error-handing and logging logic all lives in the event handler for the AppDomain.UnhandledException
event I'd suggest making a separate function that does that work which you could also call in the RunWorkerCompleted
event.
if(e.Error != null)
{
ErrorHandler.HandleCriticalError(e.Error);
}
Upvotes: 3