C# – How to debug a single thread in Visual Studio

asp.netbreakpointscmultithreadingvisual studio 2010

I have a solution with some projects. There are several break-points in different projects. I want to trace the first thread hit one of these break-points and continue tracing that single thread despite of other threads entering the same code-blocks.

I know this is possible through defining a condition on the break-point, that is, thread name = … or thread Id = … but my case is a heavy loaded ASP.NET application and as soon as I attach to w3wp.exe many threads will hit the break-points. I need some thing like a ThreadLocal<break-point>.

Is it possible? If so, how?

Best Answer

Here's what I did:

  1. Set a conditional break point that I knew would only hit on the thread that I was looking for.

  2. Once the breakpoint hits and you are in the thread you want, in the Visual Studio Threads window (while debugging, Debug -> Windows -> Threads), Ctrl + A (to select all threads), and then Ctrl + click the thread you are currently on. You should have all threads except the one you want to debug selected.

  3. Right-click, and choose "Freeze".

Now, Visual Studio will only step through the thawed thread. It seems to be much slower when doing this, presumably because it has to loop through all of the frozen threads, but it brought some sanity to my multi-threaded debugging.