Over a million developers have joined DZone.

Getting Visual Studio to Display Deadlocked Tasks

Visual Studio, since version 2010, can show tasks which are deadlocked. So, I wrote a basic program to reveal these tasks. Enjoy!

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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, 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); 



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 AutoResetEvent and waiting for another task to complete). The only task that shows as running is the one that happily burning CPU on awhile 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…

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

it analytics,visual studio,web dev

Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}