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?
Full Code:
Component:
Private Sub workerDownloader_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
RaiseEvent UpdateComplete(Me, New AsyncCompletedEventArgs(e.Error, e.Cancelled, e.Result))
End Sub
Private Sub workerDownloader_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Throw New ArgumentException("Test")
End Sub
Host app (WinForms):
Private Sub Connector1_UpdateComplete(ByVal sender As System.Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles Connector1.UpdateComplete
If e.Error IsNot Nothing Then MessageBox.Show(e.Error.ToString)
End Sub