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.
Errors
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) { Console.WriteLine("Hello World!"); Parallel.For(1, 10, (i) => RunProcess(i)); Console.ReadLine(); } static void RunProcess(int i) { Task.Delay(500).GetAwaiter().GetResult(); Console.WriteLine($"Running {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:
This gives me a familiar interface, and tells me which thread I’m currently on:
However, what I really want to see is the data local to the thread; what if I put “i” in the “Add Watch” cell:
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:
Then you can select to show only 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:
Once you’ve frozen a thread, a small pause icon appears, and that thread will stop:
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”:
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.
Stack Trace
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:
Selecting any given method will give us the active threads, and allow switching:
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:
Tasks & Threads Windows
There is a tool that allows you to view all active, blocked and scheduled tasks:
This allows you to freeze an entire task, switch to a given task, and 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:
Flags
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!
Breakpoints
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:
References
https://msdn.microsoft.com/en-us/library/dd554943.aspx
https://stackoverflow.com/questions/5304752/how-to-debug-a-single-thread-in-visual-studio