Threading not working with exception handling

badwiring

New member
Joined
Sep 23, 2004
Messages
4
Programming Experience
3-5
Hello,

I'm experiencing a problem in which an exception in a background thread jumps out of its Try/Catch block and gets handled by the application's unhandled exception handler.

I have a class that calls an overridden method (InitializeBatch) in a derived class, and it's wrapped in a Try/Catch block like this:

Public Sub Execute()
...

Try
Try
InitializeBatch()
...

Try
RaiseEvent BatchBeginning(Me, New EventArgs)
ExecuteBatch()
...

Catch ex As Exception
_TerminationCause = CauseForTermination.Failed
_FailureException = New BatchProcessFailedException(ex)
End Try
Catch ex As Exception
_TerminationCause = CauseForTermination.InitializationFailed
_FailureException = New BatchInitializeException(ex)
End Try
Finally
_Running = False
RaiseEvent BatchTerminated(Me, New EventArgs)
End Try
End Sub


My windows forms app launches this process like this:

With New Threading.Thread(AddressOf _BatchProcess.Execute)
.Name = "SMOPS_BatchProcessThread"
.IsBackground = True
.Start()
End With


For whatever reason, an exception thrown in InitializeBatch() isn't caught in the Try/Catch. Execution branches from the exception straight to the unhandled exception handler. An exception thrown in ExecuteBatch() is handled properly. I've checked and double checked, and nothing on this thread is touching the form or any controls. Is there any other common (or less common) error that could be causing this?

Thanks
Scott
 
More specifics on the actual exception:

It is a System.NullReferenceException thrown by the statement

Me.Invoke(_BatchTerminatedSub)

Message: "Object reference not set to an instance of an object."
Stacktrace: at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at SMOps.BatchProcessForm._BatchProcess_BatchTerminated(Object sender, EventArgs e) in I:\SalesAndMarketingOps\Applications\SMOps_BSC_Library\BatchProcessForm.vb:line 179

It's baffling because an exception at one point causes an event to fire, which leads the form to call Me.Invoke, and it throws this exception.
An exception a few lines later causes the exact same event to fire, leading to the exact same call the Me.Invoke, and then there is no exception.
I've tried to isolate the difference, and there appears to be none.
 
After further digging I found the problem. The error was a garden variety bug in the procedure being invoked. But that procedure didn't appear in the stack trace. It appears as if the exception were generated by the Invoke call. Because I'm relatively new to asynchronous processing I assumed it was some voodoo related to doing work on a background thread, when it was actually something much simpler.
Note to self - if Invoke throws an exception, check the procedure being invoked.
 
Back
Top