Thursday, July 4, 2013

Tasks Window in Visual Studio 2013 Preview

The Tasks Window is one of the nicest things which was introduced with Visual Studio 2013 Preview. This window will be very useful when debugging asynchronous code.

In Visual Studio 2010 and Visual Studio 2012 there is a windows called Parallel Tasks. You can view the  Parallel Tasks window either by typing Parallel Tasks in Quick Launch or by going to Debug -> Windows -> Tasks when you are debugging the code. This window is renamed to Tasks in Visual Studio 2013 Preview.

ParallelTasksWindow
Parallel Tasks Window
In addition to the functionality that Parallel Window offers in Visual Studio 2010 and 2012 in C# and VB, the Tasks window will show all of the tasks that are currently running or are scheduled to run in a asynchronous operation. Right now you might be wondering whether this Tasks window is supported only in C# and VB projects. Don't worry, this is supported for all the languages that are supported in Visual Studio for developing Windows Store applications.

You can access Task window in Visual Studio 2013 Preview by typing Tasks in Quick Launch or by going to Debug -> Windows -> Tasks and again that's when you are debugging the code. That's same as in Visual Studio 2010 and 2012.

Let’s take a look at the following code snippet.
private async void Button_Click(object sender, RoutedEventArgs e)
{
    await DoWork(http://www.microsoft.com);
} 

private async Task DoWork(string url)
{
    await Task.Yield();
    await GetString(url);
} 

private async Task GetString(string url)
{
    await Task.Yield();
    HttpClient httpClient = new HttpClient();
    string s = await httpClient.GetStringAsync(url);
}

Here I have a button and in it’s click event I am calling DoWork method asynchronously and from DoWork I am calling GetString method asynchronously. Here the awaited Task.Yield() is used to leave the current async code and to return back to the current context.

Now let’s put some breakpoints in the code. I have put two breakpoints in DoWork method and in GetString method. Now this is what you will see in the Tasks window.

OnDoWork
No Tasks in Tasks Window
There are no tasks created yet and let’s keep debugging. The moment control leaves the Task.Yield() in DoWork method, two tasks are created.

OnAwaitGetString
Tasks Window on await GetString
The task which the button click event is awaiting on is active. The task which is bound to Button click is scheduled to run. Again let’s keep debugging.

When the control comes to the line after Task.Yield() in GetString method this is what you will see in Tasks window.

OnHttpClient
Tasks Window after leaving Task.Yield in GetString
Now another task is created. Two tasks are active, the task which the Button click event is awaiting on and the task which the DoWork method is awaiting on. Still the task which is bound to Button click is scheduled to run.

And finally when you keep debugging and when the control leaves the line where the Button click calls the DoWork method, you can see that the task which is bound to Button click is now active.

OnLeavingDoWork
Tasks Window on leaving DoWork
And that’s about the Tasks window. As always love your feedback.

Happy Coding.

Regards,
Jaliya

1 comment:

  1. Nicely explained. Really helpful to understand how async work.

    ReplyDelete