As is often the case when big events happen, sometimes some important announcements can get overlooked and/or overshadowed. Mix 2011 certainly featured big news, including Silverlight 5 Beta, the Windows Phone 7.5 features, IE10, and the latest in the MVC framework. However, in all of the hoopla, the release of the SP1 refresh of the Microsoft Visual Studio Async CTP SP1 Refresh, as well as some of the important changes, may have gone unnoticed.
There are a few important things to note regarding the Async CTP Refresh:
Let me restate that last point. The Async CTP works with Windows Phone 7 today. No need to wait for the Mango update.
As a CTP, and with the “As Is” license, there is no guarantee that this will eventually make it into the framework, and even if it does, it may take a very different form. That being said, it is a great opportunity to see where things are headed and to offer feedback to the development team as to how to best shape this functionality to be as useful as it can be.
So what do the Async tools bring to the table? Among other things, it allows asynchronous code to be written without the “disruption” that currently occurs when that is being done today. While a full discussion of asynchrony and the problems it solves and the new ones it presents are beyond the scope of this discussion, some brief coverage is merited. Writing synchronous code today follows the pattern of “do something, wait for it to finish, then proceed.” Altering this to be asynchronous (often) changes the pattern either the Asynchronous Programming Model/APM (“Call BeginXXX to start the operation, then EndXXX to wait for it to be completed”) or to the Event-Asynchronous Pattern/EAP (“start doing something, and raise an event that I am listening for when you are done.”) When the completion of one asynchronous operation needs to trigger another one, code can get complicated and messy. This situation is particularly apparent to those who use Silverlight and the related Silverlight for Windows Phone, as all I/O operations in Silverlight are inherently asynchronous. The Async CTP brings a third model – the Task-based Asynchronous Pattern/TAP.
The TAP model features the use of the async and await keywords to allow asynchronous code to be written in a much more linear fashion. Under the covers, the compiler will generate all of the necessary “goo”, allowing the development to focus on the problem at a higher level. The following code may help to visualize what this all means.
In the code that follows, a WebClient is spun up to return the data at a given URL. The number of bytes in that data is returned as the result of the function. This is the synchronous version of the code.
Note that this code blocks the UI when called, and cannot be used in Silverlight or Silverlight for Windows Phone applications. So what if you don’t want to block the UI while retrieving the information (or better yet, what if you cannot, as is the case with Silverlight?) Bring in asynchrony, but notice how it changes the nature of the function. Where the desire had been to call a function to retrieve a result, now the function is called to eventually signal a response, which must be subscribed to by the calling code.
This is somewhat compressed and contained when inline lambda expressions are brought to bear, but the fundamental problem still exists. The function has been modified because of its asynchronous implementation.
Now lets see how using the Async tools change things.
Note that the functions now much more closely resemble the initial synchronous version.
This was just a light overview. There are many other combinations and applications for the TAP. Hopefully, this was enough to pique some curiosity. If interested, the download for the Microsoft Visual Studio Async CTP SP1 Refresh is available at http://msdn.microsoft.com/en-us/vstudio/async. This includes documentation, samples, and walkthroughs. The tools require Visual Studio 2010 SP1. Check out Eric Lippert’s announcement of the release (and additional links) at http://blogs.msdn.com/b/ericlippert/archive/2011/04/13/refreshing-the-async-ctp.aspx.On Apr 17 2011 5:33 PMBy jgarland With 0 Comments