BackgroundWorker RunWorkerCompleted in a Component

Posted by Sphynx on Stack Overflow See other posts from Stack Overflow or by Sphynx
Published on 2010-06-01T16:41:17Z Indexed on 2010/06/01 16:43 UTC
Read the original article Hit count: 308

Filed under:
|

I'm familiar with the following:

"If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised."

However, I've encountered a weird glitch.

In my component, there's an instance of BackgroundWorker.

Even though it's not running in the debugger, the exception remains unhandled by the worker.

Even simplified code produces an unhandled exception (and RunWorkerCompleted doesn't fire):

Throw New ArgumentException("Test")

The main thing is the code of RunWorkerComplete:

RaiseEvent UpdateComplete(Me, New AsyncCompletedEventArgs(e.Error, e.Cancelled, e.Result))

I need the component to expose the worker exception through a public event.

If I remove the RaiseEvent call, the exception becomes handled by the worker, and accessible through e.Error.

Apparently, raising an event causes the worker to miss the exception. How can that be?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about backgroundworker