Everyone who has spent time debugging errors in code that has multiple threads knows the pain of pressing F10 and seeing the cursor jump to a completely different part of the system (that is, everyone who has ever tried to).
There are a few tools in VS2017 that make this process slightly easier; and this post attempts to provide a brief summary. Obviously the examples in this post are massively contrived.
Let’s start with an error occurring inside a parallel loop. Here’s some code that will cause the error:
static void Main(string args)
Parallel.For(1, 10, (i) => RunProcess(i));
static void RunProcess(int i)
if (i == 3) throw new Exception("error");
For some reason, I get an error when a few of these threads have started. I need a tool that tells me some details about the local variables in the threads specifically. Enter the Parallel Watch Window:
Figure 1 – Launch Parallel Watch Window
This gives me a familiar interface, and tells me which thread I’m currently on:
Figure 2 – Parallel Watch Window
However, what I really want to see is the data local to the thread; what if I put “i” in the “Add Watch” cell:
Figure 3 – Add a watch
As you can see, I have a horizontal list of watch expressions, so I can monitor variables in multiple threads at a time.
Flagging a thread
We know there’s an issue with one of these threads, so one possibility is to flag that thread:
Figure 4 – Flagging a thread
Then you can select to show only flagged threads:
Figure 5 – Filter flagged threads
Freezing non-relevant threads
The flags help to only trace the threads that you care about, but if you want to only run the threads that you care about, you can freeze the other threads:
Figure 6 – Freeze Thread
Once you’ve frozen a thread, a small pause icon appears, and that thread will stop:
Figure 7 – Frozen Thread
In order to freeze other threads, simply highlight all the relevant threads (Ctrl-A) and select Freeze.
It’s worth remembering that you can’t freeze a thread that doesn’t exist yet (so your breakpoint in a Parallel.For loop might only show half the threads).
Manual thread hopping
By using freeze, you can stop the debug message from jumping between threads. You can then manually control this process by simply selecting a thread and “Switch To Frame”:
Figure 8 – Switch to Frame
You can switch to a frozen frame, but as soon as you try to progress, you’ll flip back to the first non-frozen frame (unless you thaw it). The consequence of this is that, it is possible to switch to a frozen frame, freeze all other frames and then press F10 – you’re program will then stop dead.
In a single threaded application (and in a multi-threaded application), you can always view the stack trace of a given line of executing code. There is also a Parallel Stack trace:
Figure 9 – Parallel Stacks
Selecting any given method will give us the active threads, and allow switching:
Figure 10 – Active Threads
Parallel Stack Trace – Task View
The above view gives you a view of the created threads for your program; but most of the time, you won’t care what threads are created; only the tasks that you’ve spawned (they are not necessarily a 1 – 1 relationship. You can simply switch the view in this window to view Tasks instead:
Figure 11 – Task View
Tasks & Threads Windows
There is a tool that allows you to view all active, blocked and scheduled tasks:
Figure 12 – Tasks Window
This allows you to freeze an entire task, switch to a given task, and Freeze All But This:
Figure 13 – Freeze All But This
There is an equivalent window for Threads. It is broadly the same idea; however, it does have one feature that the Tasks window does not, and that it the ability to rename a thread:
Figure 14 – Rename a Thread
The other killer feature both of these windows have is the flag feature. Simply flag a thread, switch to it, and then select “Show Only Flagged Threads” (little flag icon). If you now remove the breakpoints, you can step through only your thread or task!
So, what to do where you have a breakpoint that you might only wish to fire for a single thread? Helpfully, the breakpoints window has a filter feature:
Figure 15 – Filter Breakpoints