January 31, 2013

Understanding Async Programming

It is easy to invoke Asynchronous methods in VS 2012 RC using “asyncfeature. To use “await” operator in method you have to modified your method with “async” modifier.

Using asynchronous methods instead of synchronous methods can provide benefits. Asynchronous makes UI applications more responsive because the UI thread that launches that operation can perform other work. Asynchronous also improves the scalability of server-based application by reducing the need for threads.

The work flow in asynchronous programming is very simple as compared with synchronous programming, when we modify method with “async” modifier then during method calling control reaches an “await” expression in async method, control returns to the caller and progress in the method is suspended till awaited task completes. When the task is complete, execution can resume in the method.

Checkout this example diagram took from MSDN blog.

The numbers in above diagram corresponds to following steps.
  1. An event handler calls and awaits the AccessTheWebAsync” async method.

  2. AccessTheWebAsync” creates an HttpClient instance and the calls  the GetStringAsnc method to the download the contents of website as a string.

  3. Something happens in GetStringAsync that suspends its progress. Perhaps it must wait for a website to download or some other blocking activity. To avoid blocking resources, GetStringAsync yields control to its caller, “AccessTheWebAsync”.

  4. BecausegetStringTask” hasn't been awaited yet, “AccessTheWebAsync” can continue with other work that doesn't depend on the final result from GetStringAsync. That work is represented by a call to the synchronous method “DoIndependentWork”.

  5. DoIndependentWork” is a synchronous method that does its work and returns to its caller. 

  6. AccessTheWebAsync” has run out of work that it can do without a result from “getStringTask.AccessTheWebAsync” next wants to calculate and return the length of the downloaded string, but the method can't calculate that value until the method has the string.

  7. GetStringAsync completes and produces a string result. The string result isn't returned by the call to GetStringAsync in the way that you might expect. (Remember that the method already returned a task in step 3.) Instead, the string result is stored in the task that represents the completion of the method, “getStringTask”. The await operator retrieves the result from “getStringTask”. The assignment statement assigns the retrieved result to “urlContents”.

  8. When “AccessTheWebAsync” has the string result, the method can calculate the length of the string. Then the work of “AccessTheWebAsync” is also complete, and the waiting event handler can resume. In the full example at the end of the topic, you can confirm that the event handler retrieves and prints the value of the length result. 
To better undestand what is actually is asynchronous programming, consider the difference between synchronous and asynchronous behavior.

synchronous method returns when its work is complete (step 5), but an async method returns a task value when its work is suspended (steps 3 and 6).

When the async method eventually completes its work, the task is marked as completed and the result, if any, is stored in the task.