I've been struggling with event handling in backgroundworker threads.
All the documentation I've come across make me believe that when a DoWork event handler throws an exception that exception should be dealt with in the RunWorkerCompleted handler and that exception will be available in the Error property of the RunWorkerCompletedEventArgs.
This is fine, but during debug time I always see an exception unhandled by user code message. This makes me believe there is a problem with my approach.
What steps should I take to resolve this?
Regards, Jonathan
I've seen this behavior before, and I've gotten around it by decorating the DoWork handler with the System.Diagnostics.DebuggerNonUserCode
attribute:
[System.Diagnostics.DebuggerNonUserCode]
void bw_DoWork(object sender, DoWorkEventArgs e)
{ ... }
Note that you'll only see this if you're running in the debugger; even without the attribute, all is as it should be when running from the shell.
I looked this up again, and I still can't see any good reason why you need to do this. I'm calling it a debugger misfeature.
I've had this problem before. The e.Error only gets set when you don't run in Debug mode. If you run in Debug, exectuion stops at the spot of the Exception. However, run the same program in Non debug mode (in VS Debug -> Start Without Debugging or Ctrl+F5) and the nasty exception dialog WON'T come up, and e.Error will be the exception. Not sure why, but that's how it works....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With