If You Use BackgroundWorker in .Net, Make Sure You Wrap Your Worker Method In Try (or risk missing the exception thrown)

So, just a short post in case someone runs into the same problem I had today that cost me about 2 hours using Visual Studio 2010.  Basically, if you are using the BackgroundWorker in a windows app (with visual studio) and find that the method is not finishing and seemingly not throwing exceptions, maybe it actually is and you are missing it.

That is, if you have code like this:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
for (int i = 0; i < 10;i++ )
if (i > 5)
throw new ApplicationException("any error here");
backgroundWorker1.ReportProgress(i, "working...");

and you run it in the debugger, it will not stop by default when the exception is thrown (like it would in single threaded code).

So, I recommend doing the following and setting a break point to see the error.



Another thing you can do is bump up where debug’s stops uisng the dialog:


goes to:



Then, you will get an error you expect.




About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP since 2007. In addition, he’s a multi-course video author at Pluralsight. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


  1. Hi,

    I would suggest you should check the e.Error property in backgroundWorker1_RunWorkerCompleted event. See http://bit.ly/a9F5Lo
    One additional point I like to mention: If you want to check e.cancelled then you must check e.error before. In case of an error e.cancelled will not be boolean.
    So far Dominik

  2. Thé background worker already has a try catch it just puts it in the run completed argument. Instead of putting a try catch id handle the exception on the run completed event ans examine the error property


  1. […] If You Use BackgroundWorker in .Net, Make Sure You Wrap Your Worker Method In Try (or risk missing t… – Peter Kellner highlights the importance of handling exceptions in your background worker methods to help you avoid loosing exceptions and having background tasks terminate unexpectedly with no idea why. […]


Get every new post delivered to your Inbox

Join other followers: