Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Backgroundworker - Is there no way to let exceptions pass back normally to main thread?

QUESTION: Re use of .NET Backgroundworker, is there not a way to let exceptions pass back normally to main thread?

BACKGROUND:

  • Currently in my WinForms application I have generic exception handle that goes along the lines of, if (a) a custom app exception then present to user, but don't exit program, and (b) if other exception then present and then exit application
  • The above is nice as I can just throw the appropriate exception anywhere in the application and the presentation/handling is handled generically
like image 290
Greg Avatar asked Mar 29 '10 00:03

Greg


1 Answers

The BackgroundWorker automatically passes back the exception. It's in the AsyncCompletedEventArgs.Error property when you hook the RunWorkerCompleted event.

If you like, you can wrap and rethrow the exception in this event handler - keeping in mind that there's a bug in the framework that will cause the "outer" exception to be thrown instead because you're in the middle of an Invoke.

An exception that occurs on a background thread in a .NET application is a catastrophic error that can and will bring down the entire process; the only way to deal with this is to wrap all of the activity in a try-catch block and save any exception that occurred, which is exactly what the BackgroundWorker does.

like image 56
Aaronaught Avatar answered Sep 28 '22 09:09

Aaronaught