views:

331

answers:

5

When I am debugging within Visual Studio, for some reason when debugging a certain thread, visual studio will just jump around randomly to different threads, I assume this is the default behavior.

How do I change to behavior so it's sits on the same thread?

Why would this behavior be default? It is very annoying.

+1  A: 

Generally, I freeze the other threads by right-click in the threads panel. I don't know if this is sane or not though.

spender
+7  A: 

The behavior is the default because it's showing you the next command to be executed, which, in a threaded environment, is not the same as the current thread.

Here's how to freeze other threads you're not interested in, but be careful, because you'll avoid race conditions and other nastiness that comes from debugging multiple threads.

mmr
A: 

It is default because the running the program in the debugger shouldn't change the results of the program, I assume.

When the program is running "live", it is constantly switching between threads, so if the debugger didn't do the same, the program would be behaving differently.

In any case, the only way I know of to prevent it is to open the Threads window, right click on all other threads than the current one, and select freeze. (Remember to thaw them again afterwards)

jalf
+1  A: 

When you say, "when debugging a certain thread, visual studio will just jump around randomly to different threads", do you mean that as you step through code on a particular thread you may hit a breakpoint on a different thread?

If so, you can use the Thread window to 'freeze' threads other than the one you're interested in debugging:

From http://msdn.microsoft.com/en-us/library/w15yf86f.aspx:

From the Threads window, you can set the active thread. In addition, you can freeze or thaw the execution of each individual thread. Freezing prevents the execution of a thread. Thawing enables it to continue. Two vertical blue bars identify a frozen thread.

Support for this may depend on the version of Visual Studio you have (for example, I don't think the Express versions support the Thread window).

Michael Burr
A: 

Or right click on the next line and select "Run to cursor"

Greg Miller