This is a mirror of official site: http://jasper-net.blogspot.com/

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

| Thursday, November 11, 2010
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)
{
   backgroundWorker1.ReportProgress(0,"starting...");
   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).

Read more: PeterKellner.net

Posted via email from .NET Info

0 comments: