Hi, today I would like to show you what is a new async keyword of the C# 5.0. This keyword means that your method will be invoked asynchronously, and you can invoke it, and .NET Framework waits for results. You can simply invoke the method, and the magic happens. This blog entry will be concise. I will try only to show you one example of using the async keyword. That example is shown below. And After that, I will try to show you what compilers do with this simple example.
namespace AsyncSampleConsoleApp { using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; class Program { static async Task<string> DoWork() { Thread.Sleep(5000); return "Hello Async World!"; } static void Main(string[] args) { Console.WriteLine(DoWork().Result); Console.ReadKey(); } } }
This is very simple example. I prepared a DoWork method that returned a string and decorated this async method keyword. The async keyword can be used only for methods that return void, Task, or Task<T> type. As Implementation, I wrote will be changed by the compiler into something different, shown below.
namespace AsyncSampleConsoleApp { using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Text; internal class Program { private static void Main(string[] args) { Console.WriteLine(DoWork().Result); Console.ReadKey(); } // Methods private static Task<string> DoWork() { DoWorkTaskClass doWorkTaskClass = new DoWorkTaskClass(0); doWorkTaskClass.MoveNextDelegate = new Action(doWorkTaskClass.MoveNext); doWorkTaskClass.builder = AsyncTaskMethodBuilder<string>.Create(); doWorkTaskClass.MoveNext(); return doWorkTaskClass.builder.Task; } // Nested Types [CompilerGenerated] private sealed class DoWorkTaskClass { // Fields private bool disposing; public AsyncTaskMethodBuilder<string> builder; private int state; public Action MoveNextDelegate; // Methods [DebuggerHidden] public DoWorkTaskClass(int state) { this.state = state; } [DebuggerHidden] public void Dispose() { this.disposing = true; this.MoveNext(); this.state = -1; } public void MoveNext() { string result; try { if (this.state == -1) { return; } Thread.Sleep(0x1388); result = "Hello Async World!"; } catch (Exception ex) { this.state = -1; this.builder.SetException(ex); return; } this.state = -1; this.builder.SetResult(result); } } } }
As you can see when you install the Async CTP1 and try write simple example. And after that, when you open a compiled project into the .NET Reflector 6.8 – thanks, Read-Gate, for a great and free tool 🙂 – a method DoWork was changed into a class with a state, a builder, and a movement delegate. That class was invoked in a DoWork method without the async keyword, and the compiler created it for us. And I think I have one conclusion the ‘async’ keyword is great in most cases, but sometimes when your system needs low frequency, the amount of compiler code can be too heavy. So, again the async is great, but it is a big transformation that the compiler did for you. The async keyword looks like a language extension that is powerful and heavy. In most business applications, the async keyword of C# 5.0 will be beneficial.
Best regards,
P ;).