views:

51

answers:

1

I am currently developing a Wizard in VS2010 C#. The second to last page is a progress page with a progress bar. The work being performed takes a long time. So I have created a Backgroundworker thread and do the lengthy operation in DoWork. I update the progress bar using the ProgressChanged event. The code for the lengthy process is in a separate actions class. I call a static method in that class from the DoWork event. Exceptions are raised in the actions class.

All works fine. That is, the DoWork event successfully calls the static method from my actions class, the ProgressChanged event successfully updates the progress bar and the RunWorkerCompleted successfully traps the exceptions.

But, when I move away from the Progress page to the Finish page, the Exception error message is displayed again - without any code from me (ie. without a MessageBox.Show).

I haven't included any of my code at this stage, as it is the same as the MSDN example.

I know it has nothing to do with the working code being in a separate class or that I call a static function. I have moved the code to within the Progress page itself (as in the MSDN example) and use a private method instead of static and I still see this erroneous message.

However, if I enclose the DoWork call to my action class within a try-catch block, the erroneous message is not displayed. But then, the exception is not captured by the RunWorkerCompleted event. And besides, placing a try-catch around call in DoWorker is not the recommended way. But it's interesting this resolves the issue!

I'd be very grateful if anyone can shed some light on this?

A: 

The exception thrown from your action class will bubble up the call stack, in this case to the DoWork method of your BackgroundWoker, and then up. As you don't have a try/catch this will cause the exception to bubble up uncatched, where the VS debugger will display it to you.

  1. Either decorate your DoWork handler with System.Diagnostics.DebuggerNonUserCodeAttribute().

  2. Run the program in Release mode, not Debug

  3. In your Visual Studio go to Debug -> Exceptions and uncheck the exception being thrown.

Radu094
No, the Bgw catches exceptions from DoWork, and presents them in the Completed event.
Henk Holterman
Sorry, I should have made it clear - this is happening in Release mode. When in debug mode, the exception is caught by the debugger as explained in the MSDN example.
William