How to debug a single thread in Visual Studio?

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.


Freeze/Thaw threads is an incorrect way because other threads don't execute any code.

The most correct and usable way is to:

  1. Hit Ctrl+A in the breakpoints window (select all breakpoints).
  2. Right click and select "Filter...".
  3. Enter "ThreadId=(current thread id)".

In Visual Studio 2015 and newer, the process is similar:

  1. Hit Ctrl+A in the breakpoints window (select all breakpoints).
  2. Right click and select "Settings...".
  3. Check "Conditions" and select "Filter" in the dropdown
  4. Enter "ThreadId=(current thread id)".

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


I have just released a Visual Studio 2010+ extension that does exactly what you are looking for. And it's free :).

Presentation

This Visual Studio extension adds two shortcuts and toolbar buttons to allow developers to easily focus on single threads while debugging multi-threaded applications.

It dramatically reduces the need to manually go into the Threads window to freeze/thaw all threads but the one that needs to be followed, and therefore helps improve productivity.

Features

Restrict further execution to the current thread only. Will freeze all other threads. Shortcut: CTRL+T+T or Snowflake button. Switch to the next single thread (based on ID). Will change current thread and freeze all other threads. Shortcut: CTRL+T+J or Next button.

Check it out here on the Gallery, on the official page or the Github repository.


If multiple threads are being spawned as for a web application, @MattFaus answer's will not work. what I did instead is the following

  • Set up a breakpoint to interrupt the thread in the function I want.
  • Once the thread gets to the breakpoint and is paused, I remove the breakpoint and continue debugging using F8,F10 and F11, so that the others threads can run.