Home » .NET » Did you know that Visual Studio shows deadlocked tasks?

About Dror Helper

Dror Helper

Did you know that Visual Studio shows deadlocked tasks?

I didn’t…

And it’s not a new feature – since it existed since freaking
Visual Studio 2010!

Yesterday during an unrelated meeting
Alon Fliess showed a bunch of us developers how Visual Studio detects deadlocks in tasks (automatically) and shows the current task status in both Parallel Tasks and Tasks debug windows (duh).

I got home late but I wanted to experiment and see what would happen and so I wrote a simple program to see how well Visual Studio understands the bugs I can throw at it:

static void Main(string[] args) 
    int taskCount = 6; 
    var tasks = new Task[taskCount]; 
    var autoResetEvent = new AutoResetEvent(false); 
    for (int i = 0; i < taskCount; i++) 
        tasks[i] = Task.Factory.StartNew(state => 
            var index = (int)state; 
            if (index < 3) 
                tasks[(index + 1) % 3].Wait(); 
            else if (index == 3) 
                Task.Factory.StartNew(() => 
                    while (true) { } 
        }, i); 



What a mess – I have three tasks waiting on each other (deadlocked), two more tasks waiting indefinitely on a
AutoResetEvent and one task that starts yet another task that runs an infinite loop (just for fun).

After a few seconds I paused/breaked execution and got the following (Debug –> windows –> Tasks):


So out of the 6 tasks I spawned the first three deadlocks are detected by VS, Three tasks that show as blocked (waiting for AutoResetEventand waiting for another task to complete). The only task that shows as running is the one that happily burning CPU on a while loop that would never end (also known as the Halting problem).

That’s good enough for me.

I guess it’s another case of a useful feature that I always wanted but didn’t know already existed.

Happy coding…

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments