Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking single thread

Is it possible to break a single thread in Visual Studio, while other threads will continue their execution?

I have one background thread that does simple data sending/receiving, which I would like to happen, while stepping through my code in some other thread.

like image 542
Eugene Kulabuhov Avatar asked Aug 06 '10 10:08

Eugene Kulabuhov


People also ask

How do I see running threads in Visual Studio?

To display the Threads window in break mode or run mode While Visual Studio is in debug mode, select the Debug menu, point to Windows, and then select Threads.


3 Answers

open the thread view (Debug->Windows->Threads), right-click the thread you want to suspend, select 'Freeze'. Select 'Thaw' to put it back in a running state.

like image 96
stijn Avatar answered Oct 06 '22 11:10

stijn


Generally it's impossible, but there are some things that might work for specific scenarios.

Basic solution As mentioned elsewhere, repeating the sequence: Freeze, Resume, (wait), Pause, Thaw, Step should result in the behavior you describe, giving other threads the possibility of running in the background while your target thread is halted.

This approach has at least two issues:

  • It's quite tedious
  • Your background threads will be suspended anytime the debugger is paused.

Improvements

The first issue may be tackled using a different procedure: Issue a Thread.Sleep(10000) in the Immediate Window, effectively keeping the focused thread occupied while the other threads execute normally. You could even bind that command to a macro.

The second issue can only be tackled by an approach that does not need to pause the debugger. But how would we examine state when the session isn't paused? That's where IntelliTrace comes in, but you may find you need to create custom IntelliTrace events. Drawback of this approach is that you can not manually modify state mid-flight.

like image 8
Joseph Tanenbaum Avatar answered Oct 06 '22 09:10

Joseph Tanenbaum


Set a counter that does a one up for each thread created and then set your break point to break on a condition and pick a value for that counter. I don't think this will work in all cases, especially PLINQ, but should be doable in a lot of situations.

like image 1
stricq Avatar answered Oct 06 '22 11:10

stricq