The History
Before the introduction of these features, we generally rely on traditional approaches of creating Thread from a ThreadPool and consuming a method which needed to be asynchronously called for. Again when the Thread finishes, or during the lifetime of the Thread we need callbacks to notify UI after a while, so that the user gets an impression that the background is updating the system. This breaks the way we write or the way we think of programming. The programmer needs to create the program to device in such a way that it handles continuity of the application by writing custom callbacks to the system or even use Dispatcher to update the UI thread from background. Thus the code becomes more and more complex as we add up more functionalities to the system.
The Present
In this PDC, Anders Hejlsberg introduces two new investments on C# which is probably now still in CTP to allow asynchronous calls to the applications very simple and easy to handle. The two keyword which were introduced recently are "async" and "await". Let me introduce these two keywords for the time being.
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
async TaskGetStringsFromUrlAsync(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 TaskGetStringsFromUrlAsync(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
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 the DownloadStringTaskAsync is actually an extension which specifies to be called with async.
Async method can return void, Task or Task
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.
Conclusion
I have created the blog just to give you an introduction on what I understood about the concept on the PDC discussed today. My understanding might be totally wrong as I don't have much depth about the concept. Please let me know if I made any mistake so that I can correct it in this post. This is the very basic introduction to these new concepts, I will introduce few more extension methods and other important features that were also included with the CTP later on.
Thank you for reading my post.
How to Try ?
You can download and read more about it from :
http://msdn.microsoft.com/en-us/vstudio/async.aspx
1. you say : you have invoked 3 asynchronous in first piece of code - where ???? i can see only one
ReplyDelete2. your second piece of code doesn't compile - you declare void and return Task
Regards
Arek
@Arek
ReplyDeleteI must be sleeping while I wrote this. Actually I posted this just after the PDC, and hence could not try it personally. Sorry for this, I have updated the post now.
Thanks for your concern.
Looks a lot like F# asynchronous workflows :)
ReplyDelete@Bart Czernicki
ReplyDeleteYes Language parity is the main concern for the PDC this year. So in the future, most of the languages will have same flexibility.
Great informative post having code! I would like to implement it practically. Thanks, Keep posting!
ReplyDeleteHI
ReplyDeletereally Good.. so it is avoided call back handler .. keep posting...
@Murugan
ReplyDeleteYes.
Actually these things simplifies the callbacks but internally the compiler will make those modifications to you.
:)
Thanks for the great article and simple explaination.
ReplyDeleteQuestion:
What about the Cancellation? In .net 4.0 there is very limited support for the thread cancellation (I know only backgroundthread does have actual cancellation). Is there any support for async cancellation?
Thanks,
Binoy
@Anonymous
ReplyDeleteYou need to use CancellationTokenSource.
I have explained everything in my article on the same, I think you would like reading it too.
http://www.codeproject.com/KB/cs/async.aspx
I like this article. but your code project article is much better and explain everything in more detailed way.
ReplyDelete@Slava
ReplyDeleteYes, actually it is written just after 1 hour of PDC, so I didn't know enough of it that time.
Takk for en interessant blogg
ReplyDeleteI think, this idea is not new. It's inspired from functional programming.
ReplyDelete