X

What is Visual Studio Async?

During PDC10 Anders Hejlsberg announced Visual Studio Async CTP when speaking about new stuff in C# and VB.NET next versions. In this posting I will introduce the background of Visual Studio Async and I will provide you with links to different resources so you can get started immediately.

Why Visual Studio Async?

The main point of Visual Studio Async is to simplify asynchronous programming. If you have ever wrote some asynchronous code you know very well that it is more complex than synchronous code. And it is also longer. Instead of calling simple method using one line we have to write method that initializes asynchronous call and then we have to write callback that is called when asynchronous code has completed the work it got to do.

Smarter ones of you have also thought many times that there should be some patterns because what we are doing when writing asynchronous code that solves different problems has still some similarities. Slowly, step by step, we are now getting to the point where complexities of asynchronous code are solved by compiler and we can write asynchronous code almost like synchronous one.

How we got to Visual Studio Async?

When something new comes out it is always good to know some historical background to get better idea about what’s going on. Here is the timeline of C# and VB.NET.

The roots of Visual Studio Async are in parallel computing stuff that came after LINQ. We got parallel extensions for LINQ (PLINQ) and Task Parallel Library (TPL) when .NET Framework 4.0 came out. Task<T> is one of the main building blocks of Visual Studio Async and if you have used Task<T> before then you should be very familiar with all the backgrounds of Visual Studio Async. As .NET Framework has now all the features that enable to make asynchronous processing easier for us then Visual Studio Async will be the solution.

Requirements

To install and use Visual Studio Async your technical environment must meet the following requirements to operating system and Visual Studio:

  • Operating system: Windows 7, Windows Server 2003, Windows Server 2008, Windows Vista, or Windows XP
  • Visual Studio: Professional, Premium, or Ultimate (English-US version)

Samples

After installer of Visual Studio Async is completed you get the following page opened.

You can select examples, open them in Visual Studio and run them to see how they are written and how they work. I suggest you to start with example called 101 Asyncs. It illustrates you nicely how things are done on .NET 4.0 and how same things can be done on Visual Studio Async. Take a careful look at the following screenshot – there is Run Sample button. Yes, you can run all these samples right in the sample explorer.

Comparing .NET 4.0 and Visual Studio Async Code

Let’s see now how is the same code implemented on .NET 4.0 and how it looks like on Visual Studio Asyc. .NET 4.0 version is here.

public void AsyncIntroSerialBefore()
{
    var client = new WebClient();
 
    client.DownloadStringCompleted += DownloadStringCompleted_1;
    client.DownloadStringAsync(new Uri("http://www.weather.gov"));
}
 
void DownloadStringCompleted_1(object sender, DownloadStringCompletedEventArgs e)
{
    WriteLinePageTitle(e.Result);
 
    var client = new WebClient();
 
    client.DownloadStringCompleted += DownloadStringCompleted_2;
    client.DownloadStringAsync(new Uri("http://www.weather.gov/climate/"));
}
 
void DownloadStringCompleted_2(object sender, DownloadStringCompletedEventArgs e)
{
    WriteLinePageTitle(e.Result);
 
    var client = new WebClient();
 
    client.DownloadStringCompleted += DownloadStringCompleted_3;
    client.DownloadStringAsync(new Uri("http://www.weather.gov/rss/"));
}
 
void DownloadStringCompleted_3(object sender, DownloadStringCompletedEventArgs e)
{
    WriteLinePageTitle(e.Result);
}

And here is the same code written on Visual Studio Async.

public async void AsyncIntroSerial()
{
    var client = new WebClient();
 
    WriteLinePageTitle(await client.DownloadStringTaskAsync(
            new Uri("http://www.weather.gov"))
    );
    WriteLinePageTitle(await client.DownloadStringTaskAsync(
            new Uri("http://www.weather.gov/climate/"))
    );
    WriteLinePageTitle(await client.DownloadStringTaskAsync(
            new Uri("http://www.weather.gov/rss/"))
    );
}

When you remove these WriteLinePageTitle calls the method body is about 5 lines and it is easy to read.

Resources

Liked this post? Empower your friends by sharing it!
Categories: .NET Visual Studio

View Comments (3)

  • I'm sorry but why do we need a separate IDE for coding async calls Isn't it more of a programing paradigm like we can have MVC projects from within VS? Since you tried to cover background on Async, think you should have covered why it had to be developed as a separate product

  • Vishal, it is right now separate product. But it will be part of .NET Framework in the future. Exactly same case was with PLINQ. It was additional package before and in .NET Framework 4 it is integral part of framework. In the beginning, where some technology is new it is still good if you can give it to people so they can try it out. That's why Async is delivered as separate package right now.

Related Post