Geeks With Blogs

Michael Crump Microsoft MVP, INETA Community Champion and XAML Advocate.

Today at PDC 2010, they announced Asynchronous functions in C# and VB.NET. So what exactly does that mean? I’ll give you the definition first:

Asynchronous operations are methods and other function members that may have most of their execution take place after they return. In .NET the recommended pattern for asynchronous operations is for them to return a task which represents the ongoing operation and allows waiting for its eventual outcome.

You completely understand right? Me neither! I have been reading about it and I wanted to share my understanding of it.

Imagine an application (like the Netflix example) with a lot of data. You would experience the following behavior using a synchronous application.

  • The program becomes non-responsive.
  • You cannot move, resize the window while data is loading.
  • You cannot hit the Close button to end the program while data is loading.

We can definitely do better with asynchronous application with the new tools Microsoft provided for us.

Let’s get started by installing the Visual Studio Async CTP. I will show you how to install it, walking you through the example application and then Tips/Tricks that will save you some time when getting started.


You can download the Visual Studio Async CTP here. The C# Language Specification for Asynchronous Functions is included in the CTP or you can download it individually as well. After you have it downloaded then double click the setup file and finish installing it. After you have installed it, it will ask for a reboot. Go ahead and reboot after its finished.




After it is finished installing you will see the following screen below which contains all kind of goodies.


What we are going to do is play with the example project and convert it from a synchronous application to an asynchronous silverlight application. So click the “Find Movies” button to load the VS2010 project that we are going to work with. After VS2010 is loaded then click the “Walkthrough” link for a document Microsoft prepared to walk you through it. I am going to use the document as my guide for this tutorial.


After you have loaded the project and hit Build, then it will generate an error on the line below. 





Let’s fix the reference now. We want to add AsyncCtpLibrary_Silverlight.dll to this project. This file should be located in your “My Documents\Microsoft Visual Studio Async CTP\Samples” as shown below.


After it’s added you should see it in your references:


Now we are going to play with two new keywords, await and async (defined by Microsoft below).

Await expressions are used to suspend the execution of an asynchronous functions until the awaited task completes.

Inside asynchronous functions await expressions can await ongoing tasks, which causes the rest of the execution of the asynchronous function to be transparently signed up as a continuation of the awaited task.

In the sample project, you are only going to change two methods, LoadMovies() and QueryMovies(). You are going to refactor these methods to LoadMoviesAsync() and QueryMoviesAsync() and prefix them with the async contextual keyword. We will use await when calling our data as shown below:

async void LoadMoviesAsync(int year)
    statusText.Content = "";
    var pageSize = 10;
    var imageCount = 0;
    while (true)
        statusText.Content = string.Format("Searching...  {0} Titles", imageCount);
        var movies = await QueryMoviesAsync(year, imageCount, pageSize);
        if (movies.Length == 0) break;
        imageCount += movies.Length;
    statusText.Content = string.Format("{0} Titles", imageCount);

async Task<Movie[]> QueryMoviesAsync(int year, int first, int count)
    var client = new WebClient();
    var url = String.Format(query, year, first, count);

    // The following line doesn't compile. To learn how to fix it,
    // follow the walkthrough:
    string data = await client.DownloadStringTaskAsync(new Uri(url));
    var movies =
        from entry in XDocument.Parse(data).Descendants(xa + "entry")
        let properties = entry.Element(xm + "properties")
        select new Movie
            Title = (string)entry.Element(xa + "title"),
            Url = (string)properties.Element(xd + "Url"),
            BoxArtUrl = (string)properties.Element(xd + "BoxArt").Element(xd + "LargeUrl")
    return movies.ToArray();

If you have problems following my example then check out Page 4 of the AsyncGettingStartedWalkthrough.docx that I provided a link to earlier.

At this point, if you run the application again give it a current year (like 2007), you will notice that the program is still responsive. This is exactly why we would want to use this.


Some Tips/Tricks that I discovered while working with the Async CTP.

1) Notice the red font on async and await below? They have not been added to Visual Studio Intellisense but the program will still compile. 


2) If you lose the start page for the CTP, its can be found at : “C:\Users\User ID\Documents\Microsoft Visual Studio Async CTP\Documentation”. You can also find all of the other documentation instead of downloading each piece separately.


3) If building for Silverlight, then make sure you have the Silverlight 4 SDK and Silverlight 4 Tools for VS2010.

4) The .dll’s required to use this is located in your C:\Users\User ID\Documents\Microsoft Visual Studio Async CTP\Samples


That is everything that I wanted to show everyone. Hopefully this clears up some of the confusion and you will understand it a little better. Thanks for reading.


Note: I have included a demo of the project as well as the source code for those that need it.


alt Subscribe to my feed

Posted on Thursday, October 28, 2010 9:15 PM General Software , Silverlight | Back to top

Copyright © mbcrump | Powered by: | Join free