4

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!

Community
  • 1
  • 1
John B
  • 20,062
  • 35
  • 120
  • 170
  • I now see another answer here, but the title isn't as descriptive as mine. Does it make sense to leave this question here and just point people to the old one? Here is the link to the existing one: http://stackoverflow.com/questions/258662/unhandled-exceptions-in-backgroundworker – John B Mar 12 '10 at 20:49
  • Actually, for some reason, I never get an exception anywhere. Unlike what they said about Visual Studio breaking in the DoWork thread, mine does not!? I don't think I have any try/catches anywhere there... is there a reason it doesn't break where expected? – John B Mar 12 '10 at 20:52
  • Could I have disabled this exception from being thrown in the Exception menu in Visual Studio? I see "AsynchronousThreaAbort" under "Managed Debugging Assistants", but that didn't seem to do anything... – John B Mar 12 '10 at 20:57
  • Is there are error set in the AsyncCompletedEventArgs (http://msdn.microsoft.com/en-us/library/system.componentmodel.asynccompletedeventargs.error.aspx)? – SwDevMan81 Mar 12 '10 at 21:00
  • Yes, I am handling the error fine, it's just that Visual Studio doesn't break at the code that actually throws the exception. – John B Mar 12 '10 at 21:06

2 Answers2

7

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.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
  • Yep that does it. I must have disabled the exceptions that were being thrown by the thread. I thought it was a threading issue! – John B Mar 12 '10 at 21:52
0

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.

Justin
  • 4,002
  • 2
  • 19
  • 21