Friday, 14 April 2017

AsyncStateMachine in .NET

This article will present a custom AsyncStateMachine that shows how it is possible to create as async awaitable state machine manually. Let's first consider an easy async-await example and implement it using a custom async state machine instead of the default one in .NET. The purpose is to glance more into the inner workings of async. The following code is what is going to be implemented.

   class Program
    {
        static void Main(string[] args)
        {
            try
            {
                CallFooAsync();
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(string.Join(",",
                    ae.InnerExceptions.ToList().Select(e => e.Message).ToArray()));
            }

            Console.WriteLine("Hit any key to continue..");
            Console.ReadKey();
        }

        public static async void CallFooAsync()
        {
            int foo = await FooAsync();
            Console.WriteLine(foo);
        }

        static async Task<int> FooAsync()
        {
            await Task.Delay(5000);
            return 42;
        }


The Main method of the console application is not allowed to have async modifier, so it was necessary to introduce an extra method here. Next off, here is an example of an AsyncStateMachine at its simplest form. It is implemented as a struct.

        struct FooAsyncStateMachine : IAsyncStateMachine
        {

            private int state; 

            public AsyncTaskMethodBuilder<int> methodBuilder;
            private TaskAwaiter awaiter;

            public void MoveNext()
            {
                //State machine
                try
                {
                    if (state == 0)
                    {
                        awaiter = Task.Delay(millisecondsDelay: 5000).GetAwaiter();
                        if (awaiter.IsCompleted)
                        {
                            state = 1;
                            goto state1;
                        }
                        else
                        {
                            state = 1;
                            methodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref this);
                        }
                        return;
                    }

                    state1:
                    if (state == 1)
                    {                     
                        methodBuilder.SetResult(42);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    methodBuilder.SetException(ex);
                    return;
                }
            }

            public void SetStateMachine(IAsyncStateMachine stateMachine)
            {
                methodBuilder.SetStateMachine(stateMachine);
            }

        }

The IAsyncStateMachine interface has a method called void MoveNext(). This method is called when the async method starts up. This method can be called multiple times. It contains a state variable to control which step is next. The AsyncMethodBuilder is used to control the result returned from the async method. The method SetResult sets the result returned and the method SetException inside a catch sets an exception, if encountered. Thet method SetStateMachine sets the statemachine struct as the state machine struct itself and is also part of the IAsyncStateMachine. Now that the AsyncStateMachine is defined, it is time to kick it off.

 static Task FooAsync2()
        {
            var stateMachine = new FooAsyncStateMachine();
            stateMachine.methodBuilder = AsyncTaskMethodBuilder<int>.Create();
            stateMachine.methodBuilder.Start(ref stateMachine);
            return stateMachine.methodBuilder.Task;
        }

The AsyncMethodBuilder inside the IAsyncStateMachine is using the Create method and Start method and returns a Task. In this article the async state machine is hard coded and not made general. Of course in .NET the AsyncStateMachine used is far more complex. But creating an async state machine yourself is a nice introduction to taking a deeper look into what async really is. Async-await is rewriting your code and could have been implemented in earlier versions of .NET. Async-await not changes the functionality of your code, but the way it is executed. It provides a way to halt a method and free up the thread, for example the UI thread and run code in the background, then continue later on again. It also allow parallel code.

Resources: