Coder Perfect

In Visual Studio, how can you debug a single thread?

Problem

With some projects, I’ve come up with a solution. Different projects have a variety of break-points. I want to follow the first thread that hits one of these breakpoints and keep following it even if other threads enter the same code-blocks.

I know that defining a condition on the break-point, such as thread name =… or thread Id =…, can do this, but in my case, I have a very loaded ASP.NET application, and as soon as I attach to w3wp.exe, many threads will hit the break-points. I’m looking for something similar to a ThreadLocalbreak-point>.

Is that even possible? If so, how would you go about doing it?

Asked by Xaqron

Solution #1

Here’s how I went about it:

Only the thawed thread will be stepped through by Visual Studio now. It appears to be more slower while doing this, presumably because it needs to loop through all of the frozen threads, but it gave my multi-threaded debugging some sanity.

Answered by Matt Faus

Solution #2

Freezing and thawing threads is a bad idea because other threads don’t run any code.

The most accurate and practical method is to:

The procedure is similar in Visual Studio 2015 and newer:

So all threads are executed, but the debugger hits on the current thread only.

Answered by hzdbyte

Solution #3

I recently released a Visual Studio 2010+ extension that accomplishes exactly what you require. It’s also free:).

Check out the Gallery, the official page, or the Github repository for more information.

Answered by Erwin Mayer

Solution #4

@MattFaus answers will not work if numerous threads are created as in a web application. Instead, here’s what I came up with:

Answered by Mikaƫl Mayer

Solution #5

I’ve used a somewhat different approach:

This assumes you have enough time to complete the preceding steps before a second thread reaches your breakpoint. If other threads hit your breakpoint before you’ve done the following, you can freeze them by right-clicking them in the threads window.

Answered by Matt

Post is based on https://stackoverflow.com/questions/5304752/how-to-debug-a-single-thread-in-visual-studio