Friday 4 December 2009

BackgroundWorker - Handling Exceptions


Today i've been using Background Workers in visual studio and came to a bit of a fix when I needed to throw an expcetion within the DoWork event of the worker.

Normally, you would expect to throw exceptions as usual, but due to the nature of how the worker handles exceptions; this is done a little differently...

The worker automatically handles exceptions and passes them as an argument to the RunWorkerCompleted event. From the developers perspective, you just need to throw them within the DoWork event.
In the RunWorkerCompleted event, the RunWorkerCompletedEventArgs object contains an "Error" property. Simply test for null here to know weather an error has occurred... here you can catch your exception!

No comments: