Check out the Async CTP

Up to this point I’ve done a few posts related to implementing and using the IAsyncResult pattern.  This was a good example problem to show how some tools and techniques can be used. 

As you are probably aware, in .Net 4.0 there is a new solution for solving the problem of doing work asynchronously, in the form of the Task Parallel Library.

The Task Parallel Library makes implementing the IAsyncResult pattern trivial:

 private void Run(string[] args)
 {
     this.EndDoNothing(this.BeginDoNothing(null, null));
 }
  
 public IAsyncResult BeginDoNothing(
     AsyncCallback userCallback,
     object state)
 {
     Task t = Task.Factory.StartNew(() =>
     {
         // Do Nothing
         Thread.Sleep(1000);
     });
  
     return t;
 }
  
 public void EndDoNothing(IAsyncResult result)
 {
     Task t = (Task)result;
  
     t.Wait();
 }

So if coming up with new improvements to your implementation was a hobby of yours, you may be disappointed. Other than that, the Task Parallel Library creates a lot of opportunity to solve new problems.

Using the task parallel library is a big topic, and there are a lot of resources already on how to use it. What you may not have known is that there is an Async CTP available for download. In this post, I’d like to cover a few things I found interesting in the Async CTP.

Getting started with the Async CTP

To install the latest Async CTP, you will need to make sure you’ve installed the Visual Studio 2010 SP1 update on your machine first. After that, you can download the Async CTP from here or type "async ctp download” into Bing and it will get you right there.

The Async CTP has a lot of content to explore:

  • White papers explaining the proposed “async” and “await” keywords for C# 5.0
  • Lots of examples showing how to integrate asynchronous code with user interfaces, windows phone, and Silverlight.
  • Examples demonstrating asynchronous processing of computationally intensive work, and I/O intensive work.
  • Examples how to report progress and cancel asynchronous processing
  • A new set of APIs and white paper to support data flow

There is definitely a lot of information and lots of code to explore.  Since I don’t do much with Silverlight, UI, and windows phone currently, the parts that caught my eye were the Whitepapers, 101 Asyncs samples, and the TPL Data flow.

101 Asyncs

The 101 Async application is very clever way to present samples.  It lets you see the code while you are running it.  Here is a screenshot of the application so you can see what I mean:

Asyncs101

You can select the sample you want to try, get a description of what it is about, and run it without digging around in folders and opening project files.  In addition, it has syntax highlighting for the C# code.  You can crack open the code for this sample, and you can see how in just a few lines of code they were able to achieve the syntax highlighting.

Doing samples in a clear and consistent way in addition to creating the product is very difficult to do well. This is one of the first things I noticed when looking through the CTP.

TPL Data Flow

The data flow APIs and library let you design your application considering how the data should get processed through your application.  This is a somewhat different way of thinking about how to create applications.

Usually there is a lot of effort in creating an object model, dividing up responsibilities and data storage between the objects, and deciding on what the control flow should be.  How the data gets into and out of the objects, and into and out of the application doesn’t usually get quite the same emphasis, and there really isn’t a whole lot of tool support to help you unless you are working with SQL or a database perhaps.

The TPL Data flow looks very interesting and seems to allow you to express exactly how your data is to be processed in your application. Hopefully you will see a blog post or two from me in the future exploring this.

Summary

In this post, I wanted to show some interesting things to look at in the Async CTP.  I think you can probably spend quite a bit of time understanding everything that’s available, and understanding the code samples.  With the new support for asynchronous code, I think there is opportunity to find better solutions to existing problems, and free us up to solve more difficult programming problems.