예제 #1
0
            void IAsyncStateMachine.MoveNext()
            {
                try
                {
                    switch (_state)
                    {
                    case -1:
                    {
                        _self    = this;
                        _awaiter = Task.Delay(TimeSpan.FromSeconds(_pp)).GetAwaiter();
                        if (_awaiter.IsCompleted)
                        {
                            goto case 0;
                        }
                        else
                        {
                            _state = 0;
                            _builder.AwaitUnsafeOnCompleted(ref _awaiter, ref _self);
                        }
                    }
                    break;

                    case 0:
                    {
                        _builder.SetResult(_pp * 100);
                    }
                    break;
                    }
                }
                catch (Exception ex)
                {
                    _builder.SetException(ex);
                    //throw;
                }
            }
예제 #2
0
        public static Task <int> MyBarAsync(int pp)
        {
            MyBarAsyncStateMachine myBarAsyncStateMachine = new MyBarAsyncStateMachine
            {
                _pp = pp
            };

            myBarAsyncStateMachine._builder.Start(ref myBarAsyncStateMachine);
            return(myBarAsyncStateMachine._builder.Task);
        }
예제 #3
0
            void IAsyncStateMachine.MoveNext()
            {
                try
                {
                    switch (_state)
                    {
                    case -1:
                    {
                        _self = this;

                        Console.WriteLine($"before task run to delay {_pp} seconds");
                        _awaiter = Task.Delay(TimeSpan.FromSeconds(_pp)).GetAwaiter();
                        if (_awaiter.IsCompleted)
                        {
                            goto case 0;
                        }
                        else
                        {
                            _state = 0;
                            _builder.AwaitUnsafeOnCompleted(ref _awaiter, ref _self);
                        }
                    }
                    break;

                    case 0:
                    {
                        Console.WriteLine($"after task run and have delayed {_pp} seconds");
                        _builder.SetResult(_pp * 100);
                    }
                    break;
                    }
                }
                catch (Exception ex)
                {
                    _builder.SetException(ex);
                    //throw;
                }
            }