Post

AsyncStateMachine

This example shows what a state machine looks like after decompiling and replacing the compiler-generated variable names with simpler, more readable names.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class Dummy
    {
      
        [AsyncStateMachine(typeof (Machine))]
        [DebuggerStepThrough]
        private Task Do()
        {
            Machine stateMachine = new Machine();
            stateMachine._builder = AsyncTaskMethodBuilder.Create();
            stateMachine._this = this;
            stateMachine._state = -1;
            stateMachine._builder.Start<Machine>(ref stateMachine);
            return stateMachine._builder.Task;
        }

        public Dummy()
        {
      
        }

        [CompilerGenerated]
        private sealed class Machine : IAsyncStateMachine
        {
            public int _state;
            public AsyncTaskMethodBuilder _builder;
            public Dummy _this;
            private TaskAwaiter _u;

            public Machine()
            {
        
            }

            void IAsyncStateMachine.MoveNext()
            {
                int num1 = _state;
                try
                {
                    TaskAwaiter awaiter;
                    int num2;
                    if (num1 != 0)
                    {
                        awaiter = Task.Delay(1).GetAwaiter();
                        if (!awaiter.IsCompleted)
                        {
                            _state = num2 = 0;
                            _u = awaiter;
                            Machine stateMachine = this;
                            _builder.AwaitUnsafeOnCompleted<TaskAwaiter, Machine>(ref awaiter, ref stateMachine);
                            return;
                        }
                    }
                    else
                    {
                        awaiter = _u;
                        _u = new TaskAwaiter();
                        _state = num2 = -1;
                    }
                    awaiter.GetResult();
                }
                catch (Exception ex)
                {
                    _state = -2;
                    _builder.SetException(ex);
                    return;
                }
                _state = -2;
                _builder.SetResult();
            }

            [DebuggerHidden]
            void IAsyncStateMachine.SetStateMachine([Nullable(1)] IAsyncStateMachine stateMachine)
            {
            }
        }
    }


This post is licensed under CC BY 4.0 by the author.