[ACCEPTED]-How do I get Visual Studio to stay on one thread when debugging?-visual-studio-debugging
When you say, "when debugging a certain 17 thread, visual studio will just jump around 16 randomly to different threads", do 15 you mean that as you step through code on 14 a particular thread you may hit a breakpoint 13 on a different thread?
If so, you can use 12 the Thread window to 'freeze' threads other 11 than the one you're interested in debugging:
From the Threads window, you can set the 9 active thread. In addition, you can freeze 8 or thaw the execution of each individual 7 thread. Freezing prevents the execution 6 of a thread. Thawing enables it to continue. Two vertical 5 blue bars identify a frozen thread.
Support 4 for this may depend on the version of Visual 3 Studio you have (for example, I don't think 2 the Express versions support the Thread 1 window).
All the answers here talk about freezing 11 the threads, but it gets cumbersome when 10 there're lots of them, and you don't know 9 which one to freeze. I found an easier trick.
When 8 a breakpoint is hit by a thread i, and say 7 j, k, etc. are going to hit the same in some 6 time, then disable the breakpoint temporarily 5 and start debugging thread i. I see that 4 the debugger doesn't jump on to the other 3 threads since for those threads there's 2 no breakpoint to break into. Enable the 1 breakpoint when you're done debugging.
It is the default because running the program 10 in the debugger shouldn't change the results 9 of the program, I assume.
When the program 8 is running "live", it is constantly switching 7 between threads, so if the debugger didn't 6 do the same, the program would be behaving 5 differently.
In any case, the only way I 4 know of to prevent it is to open the Threads 3 window, right click on all other threads 2 than the current one, and select
freeze. (Remember 1 to
thaw them again afterwards)
Generally, I freeze the other threads by 2 right-click in the threads panel. I don't 1 know if this is sane or not though.
Since this question has been asked more 25 than a decade ago, the later Visual Studio 24 has a better way(?) now than freezing all 23 the threads that you don't want.
Use the 22 toolbar at the top Debug->Windows->Threads 21 or use Ctrl+Alt+H as suggested above by 20 Mr. Z to show all the Threads. Put a stop 19 point at where you want the application 18 to stop. Run it until that point is reached.
Make 17 sure this is the thread that you want. Now 16 go to the Threads window to click on the 15 flag symbol on the left
of the thread that 14 you want. On top of this window are one 13 drop-down button and one toggle button both 12 with a flag symbol on. Use the toggle button 11 or Ctrl+9 to Show Flagged Threads Only. Now click in the code editor 10 window of where you want the code to go 9 next, right-click and choose Run Flagged Threads to Cursor and the debugging 8 will stay in the same thread(s) that you 7 want.
(Also you need to remove stop points 6 first before using the right-click Run Flagged Threads to Cursor method 5 or they will still stop other threads at 4 those points.)
You can also read about it 3 on Microsoft's documentation here. The section 2 "Flag and Unflag Threads in Source 1 Code" is the important part.
More Related questions