void IAsyncStateMachine.MoveNext()
        {
            _acc++;
            int innerTmpResult;

            try
            {
                TaskAwaiter <int> awaiter;

                if (this.State != State.Run)
                {
                    Info.WhichThread("state not run", this._acc);

                    this._task = new Task <int>(() => StateMachineAsyncAwait.Sum(this.Args1, this.Args2));
                    this._task.Start();
                    awaiter = this._task.GetAwaiter();

                    if (awaiter.IsCompleted == false)
                    {
                        Info.WhichThread("awaiter is completed=true", this._acc);

                        this.State        = State.Run;
                        this._taskAwaiter = awaiter;


                        GenerateStateMachine stateMachine = this;
                        this.AsyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
                        this._taskAwaiter.OnCompleted(this._action);

                        return;
                    }
                }
                else
                {
                    Info.WhichThread("state run", this._acc);

                    awaiter           = this._taskAwaiter;
                    this._taskAwaiter = new TaskAwaiter <int>();
                    this.State        = State.BeforeRun;
                }

                this._result   = awaiter.GetResult();
                innerTmpResult = this._result;
            }

            catch (Exception ex)
            {
                this.State = State.End;
                this.AsyncTaskMethodBuilder.SetException(ex);
                return;
            }

            Info.WhichThread("state end", this._acc);
            Console.WriteLine($"acc-{this._acc}");

            this.State = State.End;
            this.AsyncTaskMethodBuilder.SetResult(innerTmpResult);
        }
Beispiel #2
0
        public static void Main()
        {
            StateMachineAsyncAwait
            .SumAsync(10, 10)
            .ContinueWith((t) => Console.WriteLine(t.Result));

            //SumAsync(10, 10)
            //    .ContinueWith((t) => Console.WriteLine(t.Result));

            Console.ReadKey();
        }