John B
John B

Reputation: 20350

How can I debug an unhandled exception in code called from a BackgroundWorker?

I am running some import code asynchronously from a simple WinForms app using a BackgroundWorker object and its DoAsync() method. I had a problem where I didn't know that exceptions were being thrown and the thread was prematurely dying. I eventually discovered this, and now know when an exception is thrown after reading Unhandled exceptions in BackgroundWorker.

However, I still have a problem while debugging. How do I debug this code? I guess I could run it in a test app that doesn't use a BackgrounWorker, but is there a way to debug this as is? If I step through the code that actually throws the exception, I just get kicked out the step-through when the exception occurs. Re-throwing the exception from the RunWorkerCompletedEventHandler naturally doesn't help much either.

Any ideas!? Thanks in advance!

Upvotes: 4

Views: 1949

Answers (2)

Justin
Justin

Reputation: 4072

Visual Studio will only let you step into the code if you have the source. If you don't have the source then I suggest learning how to use winDBG. MS has an excellent tutorial here:

http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx

It has a learning curve and takes some time but I have found it to be worth it. I've used this tool to debug A LOT of applications that suddenly crash, hang the CPU, or just perform poorly.

Upvotes: 0

Andrew Bezzub
Andrew Bezzub

Reputation: 16032

Why not use "Break On Exceptions" option of VS (Ctrl + Alt + E or Debug->Exceptions and put checkbox in Thrown column)? This will stop your program execution when any exception is thrown.

Upvotes: 7

Related Questions