I had to fly out early on Friday, so the only session I attended on Friday was Mads Torgersen and Alex Turner’s talk on the async features in C# 5.0. I’ll start with the punchline:
In C# and async, you’ll never write another callback.
In a nutshell, that’s what these features add to the language: Transformations so that you can write code that looks and reads like today’s synchronous code, and the compiler will transform that into the necessary constructs for async behavior. The compiler will create the state machine, and write the callback code for you.
There is, however, one very important distinction:
C# / .NET tasks are created “hot”, meaning they start immediately. WinRT async methods are created “cold”, meaning they don’t start until you await them.
The .NET languages are leveraging Task<T> and extension methods which take or return Task<T> to make the operation of WinRT async methods behave as similar as possible to C# async methods. For example, you can use the Start() and StartAsTask() extension methods to start a WinRT async operation. StartAsTask() also massages the WinRT awaitable to a C# Task<T> so that it supports cancellation.
Task<T> is also used to provide some other useful async constructs. Task.Run() enables you to write a compute intensive task for a background thread, and leverage the await pattern. The library handles the thread marshaling so that the continuation executes on the foreground thread when the background thread finishes its work.
Task<T> also contains the Task.Delay() method that you can use to easily check for timeouts expiring in long running operations.
Admittedly, this is a short post. Watch their talk, and try it out yourself.
You’ll never write another callback.
All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.