Debugging Asynchronous Code in Visual Studio 2013 - Tasks Window

Debugging Asynchronous Code in Visual Studio 2013 - Tasks Window

Rate This
  • Comments 4

In an earlier post, I described how enhanced call stacks in Visual Studio 2013 can be used to help you debug asynchronous code. In this second post, I will describe the additional asynchronous debugging improvements through the Tasks window in C#/VB, JavaScript, and C++.

The Parallel Tasks window was introduced in Visual Studio 2010 to give user a tasks oriented view of their application similar to the Threads window. In Visual Studio 2013, this has been renamed to the Tasks window and is upgraded to aid Windows developers in understanding the state of all tasks and promises running in their application. This can be useful for debugging hung and excessively long tasks. Like the upgraded call stacks, this is supported for all languages that are supported in Visual Studio for developing Windows Store applications.

 

C# and VB

In addition to the functionality that the window offers in Visual Studio 2010 and 2012 in VB and C#, the window will show all of the tasks that are currently running or are scheduled to run which represent asynchronous operations.

Consider the following C# code:

  private async void Button_Click(object sender, RoutedEventArgs e)
  {
       await DoWork(10);
  }    
        
  private async Task DoWork(int a)
  {
       await Task.Yield();
       int i = a*a;
       await GetFile(i);
  }

  private async Task GetFile(int a)
  {
       await Task.Yield();
       StorageFolder folder = KnownFolders.DocumentsLibrary;
       StorageFile file;
       file = await folder.GetFileAsync(a.ToString() + ".txt");    
  }

If you are stopped in the continuation part of the GetFile method at the highlighted line above, you will see the following tasks in the Tasks window.

ManagedTasks

The Start Time represents the time when the task was created relative to the time that you started debugging. The Duration is how long that the task has been running for. This information can help you understand the execution order of your asynchronous tasks and to find which ones may have been running for longer than expected. The location column shows the current location in code. By hovering over the location, you will see a call stack including the asynchronous calls. You can click on the individual frames to navigate to the code. The Task column helps you identify and distinguish between different tasks. For example, “Async: <GetFile> d_8” is the lambda function in the continuation of the GetFile asyncronous method.

Hovering over “Awaiting” status allows you to see which task is being awaited by the current task.

 ManagedTasks2

 

JavaScript

This window did not support JavaScript at all in previous releases, but now with VS2013 the debugger can show tasks that are active. In the example below we are debugging a JavaScript app that makes an asynchronous call into a custom WinRT component (similar to this walkthrough). When we break into the app we don’t immediately see that our call has not completed unless we debug native code.

JavaScriptBeforeTasks

Opening the Tasks window allows us to see that we have an unfulfilled promise and we can navigate to our JavaScript code that created it. As in the C# case the Location column can be hovered over and the call stack tooltip is navigable.

JavaScriptTasks

 

C++

In addition to all of the features in the Parallel Tasks window under C++ debugging, the new Tasks window has additional fields about when a task was created and for how long it’s been scheduled. Consider the example shown below. In this simple Store application, four tasks are created and executed with a breakpoint in the second task. When the breakpoint is hit, we quickly see the state of all active or scheduled tasks in the Tasks window.

CppTasks

The status column shows whether a C++ task has been scheduled or is actively running (completed tasks are not displayed). Start time and duration display when the task began relative to the start of the app and how long it’s been since it was scheduled, in seconds. The location column contains the top of the thread’s call stack and its tooltip displays the full call stack, which is navigable. Additionally, double-clicking anywhere in the row will navigate to the tasks context.

Note: The new C++ Tasks window features are only available for C++ Store applications. For non-Store C++ applications, the behavior is the same as it was for the previous version of Visual Studio.

We would love to hear any questions or comments you have in the comments below or on our MSDN forum.

Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post
  • the Tasks window does not seem to show anything when debugging WP8 Projects?

  • @phil

    Unfortunately, this functionality is not currently supported for Windows Phone projects.

  • Brad, any clue who owns the "Tasks" window? I'm using VS2013 RTM and am unable to load the windows. Get error:

    The component 'Microsoft.VisualStudio.Debugger.Parallel.UI.ParallelTasksUI' does not have a resource identified by the URI '/Microsoft.VisualStudio.Debugger.Parallel;component/ui/paralleltasks/paralleltasksui.xaml'.

    A connect issue has been opened:

    connect.microsoft.com/.../801671

  • @Jack - we are tracking the issue in this bug: connect.microsoft.com/.../parallel-tasks-view-doesnt-open

    Please provide any further info there!

    Thanks!

Page 1 of 1 (4 items)