Esempio n. 1
0
        public static Task <int> MethodManualAsync()
        {
            bool debgugToSleep = true;
            MethodManualAsyncStateMachine stateMachine = new MethodManualAsyncStateMachine(debgugToSleep);

            return(stateMachine.Start());
        }
Esempio n. 2
0
            public Task <int> Start()
            {
                MethodManualAsyncStateMachine self = this;

                _atmBuiler.Start(ref self);
                return(_atmBuiler.Task);
            }
Esempio n. 3
0
            public void MoveNext()
            {
                switch (_state)
                {
                case 1:
                {
                    _state = 2;

                    PrintLine(sb, ch, count);
                    Console.WriteLine("Begin async method");
                    Console.WriteLine();
                    //rr += await MyDelayAsync(loop, milsec);
                    _awaiter = MyDelayAsync(loop, milsec).GetAwaiter();
                    if (_awaiter.IsCompleted)
                    {
                        goto case 2;
                    }
                    else
                    {
                        if (_debgugToSleep)
                        {
                            MySleep(3600 * 3, 1000);
                        }
                        _self = this;
                        //_atmBuiler.AwaitOnCompleted(ref _awaiter, ref _self);
                        _atmBuiler.AwaitUnsafeOnCompleted(ref _awaiter, ref _self);
                    }
                }
                break;

                case 2:
                {
                    _state = 3;

                    rr += _awaiter.GetResult();

                    ++ch;
                    PrintLine(sb, ch, count);
                    Console.WriteLine($"The result is {rr}");
                    Console.WriteLine();
                    loop += delta;
                    //rr += await MyDelayAsync(loop, milsec);
                    _awaiter = MyDelayAsync(loop, milsec).GetAwaiter();
                    if (_awaiter.IsCompleted)
                    {
                        goto case 3;
                    }
                    else
                    {
                        if (_debgugToSleep)
                        {
                            MySleep(3600 * 3, 1000);
                        }
                        _self = this;
                        //_atmBuiler.AwaitOnCompleted(ref _awaiter, ref _self);
                        _atmBuiler.AwaitUnsafeOnCompleted(ref _awaiter, ref _self);
                    }
                }
                break;

                case 3:
                {
                    _state = 4;

                    rr += _awaiter.GetResult();

                    ++ch;
                    PrintLine(sb, ch, count);
                    Console.WriteLine($"The result is {rr}");
                    Console.WriteLine();
                    loop += delta;
                    //rr += await MyDelayAsync(loop, milsec);
                    _awaiter = MyDelayAsync(loop, milsec).GetAwaiter();
                    if (_awaiter.IsCompleted)
                    {
                        goto case 4;
                    }
                    else
                    {
                        if (_debgugToSleep)
                        {
                            MySleep(3600 * 3, 1000);
                        }
                        _self = this;
                        //_atmBuiler.AwaitOnCompleted(ref _awaiter, ref _self);
                        _atmBuiler.AwaitUnsafeOnCompleted(ref _awaiter, ref _self);
                    }
                }
                break;

                case 4:
                {
                    _state = -1;

                    rr += _awaiter.GetResult();
                    ++ch;
                    PrintLine(sb, ch, count);
                    Console.WriteLine($"The result is {rr}");
                    Console.WriteLine("End async method");
                    Console.WriteLine();
                    _atmBuiler.SetResult(rr);
                }
                break;
                }
                //throw new NotImplementedException();
            }