The async keyword implementation in the C# language

Hi, today I would like to show you what is a new async keyword of the C# 5.0. This keyword really mean that your method will be invoked asynchronously and you can invoke it and .NET Framework waits for results, you can simply invoke method and the magic happen. This blog entry will be very short I try only show you one example of using async keyword. That example is shown below. and After that I try to show you what compiler 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 return string and I decorated this method async 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 that is 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 moveNext delegate. That class was invoked in a DoWork method without the async keyword and compiler create it for us. And I think I have one conclusion that  the ‘async’ keyword is great in most case, 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 language extension that is powerful and heavy. I think that in most business applications async keyword of C# 5.0 will be very useful.

Best regards,

P ;).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.