Run your business on the best data platform available

New for Windows Phone (Now) – the Async CTP

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:

  • It includes an “As-Is” Go-Live license for use in production environments (albeit with some stern warnings about why you should really consider NOT using it in production code.)
  • It includes support for .Net, Silverlight 4, and Windows Phone 7.0.

Let me restate that last point.  The Async CTP works with Windows Phone 7 today.  No need to wait for the Mango update.

Now Just “await” a Minute…

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.

That’s Nice.  How Long do I have to “await” to See Some Code?

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.

Code Snippet
  1. private void button1_Click(object sender, RoutedEventArgs e)
  2. {
  3.     var count = DoSomething();
  4.     MessageBox.Show(count.ToString());
  5. }
  6. private Int32 DoSomething()
  7. {
  8.     var webClient = new WebClient();
  9.     var data = webClient.DownloadString(new Uri(“https://training.atmosera.com/Consulting/Consultants/John-Garland”));
  10.     var results = data.Length;
  11.     return results;
  12. }

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.

Code Snippet
  1. private void DoSomethingEAP()
  2. {
  3.     var webClient = new WebClient();
  4.     webClient.DownloadDataCompleted += HandleWebClientDownloadDataCompleted;
  5.     webClient.DownloadStringAsync(new Uri(“https://training.atmosera.com/Consulting/Consultants/John-Garland”));
  6. }
  7. private void HandleWebClientDownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
  8. {
  9.     var data = e.Result.Length;
  10.     MessageBox.Show(data.ToString());
  11. }

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.

Code Snippet
  1. private void DoSomethingEAPLambda()
  2. {
  3.     var webClient = new WebClient();
  4.     webClient.DownloadDataCompleted += (o, e) =>
  5.     {
  6.         var data = e.Result.Length;
  7.         MessageBox.Show(data.ToString());
  8.     };
  9.     webClient.DownloadStringAsync(new Uri(“https://training.atmosera.com/Consulting/Consultants/John-Garland”));
  10. }

Now lets see how using the Async tools change things.

Code Snippet
  1. private async void button1_Click(object sender, RoutedEventArgs e)
  2. {
  3.     var count = await DoSomethingAsync();
  4.     MessageBox.Show(count.ToString());
  5. }
  6. private async Task<Int32> DoSomethingAsync()
  7. {
  8.     var webClient = new WebClient();
  9.     var data = await webClient.DownloadStringTaskAsync(new Uri(“https://training.atmosera.com/Consulting/Consultants/John-Garland”));
  10.     var results = data.Length;
  11.     return results;
  12. }

Note that the functions now much more closely resemble the initial synchronous version.

I’m Tired of “awaiting”.  Get Me the Bits.

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.

Optimize Licensing Based on Needs.

We partner with your team to document needs and the most effective combination of editions to use. This includes helping you make sure you are not using too many or too few licenses. We can make sure you are up to date ahead of any Microsoft audits to avoid possible fines and back license charges.

Drive your Roadmap Evolution.

Operate and Maintain Databases.

Accelerate Your Path to Azure.

We bring expertise to expand the performance, on demand scalability and security of your databases.

Rely on us to deploy your licenses, monitor performance and perform all necessary patching.

Leverage deep knowledge of features available and how to deploy your databases across private, public and hybrid Azure.

Why SQL Server?

  • Delivers the most secure and least vulnerable database
  • Provides ground-breaking performance and scale
  • Boasts the fastest In-Memory technology on the planet across workloads
  • Introduces accelerated hybrid cloud scenarios with Azure
  • Provides platform choice – Windows and Linux for any type of data and application.

How Atmosera Brings Value to SQL Server

  • Provide migration to SQL Server 2016 and take advantage of the latest features
  • Get better performance, monitoring, security and access control, and data protection.
  • Deploy in a Hybrid environment with the ability to stretch and create temporal databases in Azure.
  • Leverage native advanced Business Intelligence (BI) and analytics tools.
  • Optimize database abd data warehouse deployments to safeguard your data.
  • Let our team do all the architecture and DBA work – read about our DBA as a Service.

a

We deliver solutions that accelerate the value of Azure.

Ready to experience the full power of Microsoft Azure?

Start Today

Blog Home

Stay Connected

Upcoming Events

All Events