Debugging Asynchronous Code in Visual Studio 2013

Posted by Filip Ekberg on 15 Nov 2013

With Visual Studio 2013 being publicly released I think it's time I show off one nice improvements in Visual Studio 2013.tudio 2013. There are in fact a lot of nice improvements in Visual Studio 2013 and one of my favourite ones, actually two of my favourite ones are the debugging improvements of asynchronous code and the information that we are given about for instance how many references there are to our method(s).

You've been able to see the current executing threads for a while now in previous versions of Visual Studio, but now you can see a breakdown of the current tasks that are being executed. While you are debugging your application you can go ahead and open up the new Tasks window. Without having to navigate yourself around the endless menus in Visual Studio, you can go up to the right corner (don't click the X!) and type Tasks in the search box, you should see something like the following:

ShowingTasks

I put together a fairly simple code snippet to show some data in the new window that we have access to in order to get improved information about the running tasks and the sample looks something like this:

class AmazingApi
{
    public async Task ExecuteAsync()
    {
        var tasks = new[]
        {
            Task.Delay(600),
            Task.Delay(200),
            Task.Delay(100),
        };

        Task.Run(async () =>
            {
                await Task.Delay(500);
                await Task.Delay(1000);
            }
        );

        await Task.WhenAll(tasks);
    }
}

Notice that the method doesn't really return anything but it's best practice to return a Task instead of void, I recently did a talk on why that is so if you want to know that go and have a look! Another thing to notice is that I follow the method naming convention and appending the word Async to the method name.

Alright, let's go back to the window that we are looking for now, I set a breakpoint, told Visual Studio to start a new debugging instance and this is what I get now:

AsyncImprovementsInVs2013

The Tasks window gives us tons of important information such as:

  • Task Id
  • Status
  • Start time
  • Duration
  • Where the code is located
  • What task it is (notice the state machine for the async method)

If you find yourself writing a lot of asynchronous code, this will be really helpful!

Do you have a favourite feature of Visual Studio 2013 yet or are you stuck at an older version of Visual Studio and miss some of the new features? I'd love to hear your comments!

comments powered by Disqus