Debugging Parallel Code in Visual Studio

Posted by: Suprotim Agarwal , on 3/26/2014, in Category Visual Studio
Views: 39500
Abstract: The tools for Parallel debugging in Visual Studio 2012/2013 are pretty awesome. Find out how!

.NET 4.0 introduced the Task Parallel Library (TPL) and Parallel LINQ (PLINQ) in an attempt to make parallel programming simpler and making best use of multi-core processors easier. Recently I was playing around with the Parallel.Foreach and the new Enumerator APIs for the File System in System.IO trying to build a Fast Folder Scanner when I chanced upon the Parallel debugging options in Visual Studio 2012/2013. After fiddling around a little bit, I was able to make sense of the information and it was kind of a ‘brain explode’ moment.

Let me share the things that I figured out.



The Harness Code

- Let’s create a Console Application called FastFolderScanner

- Next we put together the following code to scan folders for a particular type of file and split them out.

static void Main(string[] args)
    ScanFolders(new System.IO.DirectoryInfo(@"C:\Users\Public\Documents\My Projects\Github\"));

private static void ScanFolders(System.IO.DirectoryInfo dirInfo)
        IEnumerable<System.IO.FileInfo> files = dirInfo.EnumerateFiles("*.cs");
        Parallel.ForEach<FileInfo>(files, WriteName);
        IEnumerable<DirectoryInfo> directories = dirInfo.EnumerateDirectories();
        if (directories.Count<DirectoryInfo>() > 0)
            Parallel.ForEach<DirectoryInfo>(directories, ScanFolder);
    catch (Exception ex)
        Console.WriteLine("ERROR: " + ex.Message);

private static void ScanFolder(DirectoryInfo currentFolder, ParallelLoopState arg2, long arg3)

private static void WriteName(FileInfo currentFileInfo, ParallelLoopState arg2, long arg3)

- The ScanFolders method is the crux of code. The main method sends in the starting folder and it’s DirectoryInfo object

- Using the DirectoryInfo we will pull an Enumerator out for a particular file type

- Then we will use the Parallel.Foreach from the TPL to print out the names in the current folder

- Next we will use the retrieve the DirectoryInfo enumerator in the current folder

- In the second Parallel.Foreach of the method, spin off a recursive call to the current directory via the ScanFolder method (which calls back into ScanFolder for any given DirectoryInfo)

- This spawns a nice tree of threads for us to visualize

Visualizing the TPL Execution in Visual Studio Debugger

Note: Any capable modern system will most likely spin through a few thousand files in a blink. So experiment around with a Thread.Sleep(xyz) in the WriteName method after the Console.WriteLine to ‘slow’ things down for academic purposes.

In the above code, I’ve put my local Git Repo and asked it to look for cs files and we can rest assured there are ‘lots’ of them.

1. We run the application

2. Switch to Visual Studio quickly and hit Break All or Ctrl+B. Let’s assume the execution broke in the ScanFolders method


The Parallel Tasks Window

3. Go to Debug > Windows > Parallel Tasks to bring up the Parallel Tasks window. You are likely to see a trace like the following. This shows us the Thread assignment, Thread ID, Status etc. Note the Flag column, you can actually flag a particular Thread and watch it specifically too.


The Parallel Watch Window

4. Now do the ‘Continue and Break’ routine till you break in the WriteName method. If you are repeatedly hitting the ScanFolders method, after the first break, put a breakpoint in the WriteName method and let it hit the breakpoint. Go to Debug > Windows > Parallel Watch > Parallel Watch 1. You should see a window similar to this one.


5. The above doesn’t give us much info about the value of parameters in the thread. So select the ‘currentFileInfo.FullName’ and select ‘Add Parallel Watch’


This will add a window similar to the following one


As you can see, you will be able to see the filename that is being parsed by the active threads.

The Parallel Stack Visualization

So far we have seen the parallel task window and watch window. But we often refer to our stack traces to see where the call is coming from. In parallel executing, a linear stack trace doesn’t help much, so Visual Studio provides a graphical chart. You can invoke it from Debug > Windows > Parallel Stacks. This opens up a Window similar to the following:


Look at it and let it sink in. This is hardcore stuff. You actually have the stack trace of each thread being executed. You can hover over code (e.g. Program.ScanFolders) to see the StackFrame


Then double-click on the stack and navigate to the code.


Next you can evaluate the files in the enumerator.


The ‘Brain Explode’ moment!! Awesome stuff.


Even though the sample we saw today was academic, the tools for Parallel debugging in Visual Studio 2012/2013 is pretty awesome and will definitely help save pesky issues that crop up when doing complex operations in parallel.

Traditionally parallel programming has been considered tough and reserved for the Coding Ninjas only. But with the introduction of TPL supported by the awesome debugging tools, the bar of entry into parallel programming is getting lowered (yeah, very slowly, I know :), but the intern-Ninjas have a lot of help now in form of tooling support).

If you enjoyed reading this article, check another one from Subodh Sohoni that demonstrates some Code Optimization Tools in Visual Studio 2013

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!