How do I get Visual Studio to stay on one thread when debugging?
Asked Answered
C

5

46

When I am debugging within Visual Studio, for some reason when debugging a certain thread, Visual Studio will just jump around to different threads.

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

Crosse answered 1/7, 2009 at 0:30 Comment(0)
F
30

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).

Freshman answered 1/7, 2009 at 0:41 Comment(2)
While debugging, when I hit a breakpoint, I go the thread-window, freeze all threads, and only thaw the current thread. Like this, I can continue debugging only the thread I'm working on.Sobel
For those unable to find this window, go to the toolbar > Debug > Windows > Threads (or Ctrl + Alt + H). From there you can right click on a thread and freeze/thaw it.Cornuted
T
12

All the answers here talk about freezing the threads, but it gets cumbersome when there're lots of them, and you don't know which one to freeze. I found an easier trick.

When a breakpoint is hit by a thread i, and say j, k, etc. are going to hit the same in some time, then disable the breakpoint temporarily and start debugging thread i. I see that the debugger doesn't jump on to the other threads since for those threads there's no breakpoint to break into. Enable the breakpoint when you're done debugging.

Taffy answered 21/1, 2014 at 9:49 Comment(1)
the problem is if you're debugging something where one thread passes of data to another...Hacking
Y
7

It is the default because 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)

Yb answered 1/7, 2009 at 0:37 Comment(2)
This makes sense... but doesn't change the fact that it's highly annoying. :) It would be helpful for debugging if MS added a config option to stay within the current thread when the breakpoint is hit... that way, the logic that is executing at that time can be debugged, without the cumbersome-ness and associated lag when debugging a program with frozen threads.Bias
I don't disagree but an obvious counter argument is that when you're debugging, you're not working with your real production application either, because you likely haven't compiled your debug version application with optimizations on. If you're looking for a logic problem, having better control of the debugger is far more useful than being pedantically correct.Hacking
T
4

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

Trahan answered 1/7, 2009 at 0:36 Comment(0)
P
0

Yes, only way is to freeze all but the thread you want to debug:

  • Open the Threads window by going to Debug > Windows > Threads.
  • In the Threads window, right-click on the threads you want to freeze and select Freeze. This will prevent the debugger from switching to those threads.

However they did not think about the fact that, if you let the only thread you are debugging to end, the debugger will have no thread to switch to and the whole debug session will be locked as you will not be able to thaw any of the other threads.

Peat answered 13/6, 2024 at 10:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.