Nov 18, 2015

Asynchronous Programming with Async and Await (MSDN) + Extra

https://msdn.microsoft.com/library/hh191443.aspx

https://code.msdn.microsoft.com/Async-Sample-Example-from-9b9f505c


http://www.abhisheksur.com/2010/10/c-50-asynchronous-made-easy.html




(Excerpted from http://www.abhisheksur.com/2010/10/c-50-asynchronous-made-easy.html)



What is await ? 

According to Whitepaper published on the CTP, await keyword lets you to wait your program and lets the program to be declared as a continuation of all the existing awaits so that the compiler automatically waits for all other awaits to be finished until the program continues its execution. 
In other words, say you have invoked 3 asynchronous methods from your code an asynchronous block from your code, you would just write the way you would have written this synchronously with a single keyword await before the call. The call lets the compiler wait until the execution successfully completes. Just as shown below : 
async Task GetStringsFromUrlAsync(string url)
{
     WebClient client = new WebClient();
     return await client.DownloadStringTaskAsync(new Uri(url));
}

This involves the call to DownloadStringTaskAsync which is already implemented for WebClient. Now as you can see I have specified the await just before the call to Asyncrhonous method DownloadStringTaskAsync, it will keep the call on wait. 

Even though if I say use :

async Task GetStringsFromUrlAsync(string url)
 {
      WebClient client = new WebClient();
      Task<string> modifieduri = client.DownloadStringTaskAsync(new Uri(url));
      string result = await modifieduri;
      return await client.DownloadStringTaskAsync(new Uri(result));
}

It means the second Task depends on the first one. So in this case the First DownloadStringTaskAsync will be called. This call actually returns Task(Task for any other operation) which you can keep on wait until it is finished using await keyword. So when you execute, the first call will block until the result is fetched and then it will take the string result and call the next one. 

Thus you can see how you can use await on Task variable to wait the object for completion without any implementation of custom callbacks, global objects etc.


What is async? 

Now, how to implement the asynchronous method? Hmm, async keyword can be placed just before the function call to make the method asynchronous. 

By specifying the async keyword against your method call, the compiler will automatically select your method to be asynchronous, which means it will move to the next line before returning the result in parallel until it finds an await. In the example I have provided theDownloadStringTaskAsync is actually an extension which specifies to be called with async.

Async method can return void, Task or Task based on what you need in your environment. But it is mostly preferable to return at least a Task object so that the caller can better handle the call. 

For any async method, you should put await before fetching the result, otherwise you might end up with blank/null result. await works with async to stop the further execution before finishing the other operations in context.