views:

87

answers:

2

When I'm debugging managed, multi-threaded code and I'm at a breakpoint in the Main\UnitTestRunner thread and start single stepping occasionally the debugger will switch to another thread and start stepping it's code. How can I stop this?

Using VS2010 + Resharper 5.1 if it matters.

Update 1:

A closer look at the stack after a single step (F10) command shows that many (10-30) steps were actually performed before the debugger decided to stop execution.

This is happening regularly enough to make debugging nearly impossible. This is scarily similar to the issue Hans referred to.

Update 2:

I've filed a bug on Microsoft Connect. Please "upvote" it if you are also having this issue.

Update 3:

Confirmed by M$ as a bug to be fixed in the next "servicing release." Workaround is to target unit tests to .NET 3.5 as described here.

+4  A: 

You can go to the thread view and freeze the other threads.

Debug -> Windows -> Threads

Then right click on the threads you want to Freeze and select Freeze. When you want that thread to be used again right click the thread and select Thaw.

Brian R. Bondy
Does Freeze keep the debugger from stepping into the thread, or stop the thread from running at all? I need for the background threads to keep running, I just don't want to debug them.
David Lynch
@David Lynch: The threads can't continue to run in the background because then you could encounter race conditions in your debugging that don't replicate actual program execution. It seems that Visual Studio attempts to keep threads running with events in the same order in the debugger that they'd be in actual program execution - you can freeze threads you're not using, but your program may expect those threads to have accomplished something (or, conversely, if they're left running, they may expect something to be done that you're currently debugging), so be cautious debugging like that.
rwmnau
@rwmnau: Multiple threaded apps are inheriently asynchronous, and the threads will run as such, even in the debugger.
David Lynch
@Brian: I can't freeze the threads in question as they must run for main thread to enter the code I am debugging.
David Lynch
@David: You should be able to freeze and thaw as needed. See the note from Hans Passant above perhaps.
Brian R. Bondy
A: 

In addition to Brian's answer - if the code in your threads does not overlap, you can click on the next line at which you want to stop and hit "Run to Cursor"

Steve Townsend