Ejemplo n.º 1
0
        public static System.Threading.Tasks.Task<long> GetStateMachine()
        {
            Action moveNext = null;
            var builder = new AsyncTaskMethodBuilder<long>();
            int state = 0;
            var taskAwaiter = GetTaskResult().GetAwaiter();

            moveNext = () =>
            {
                switch (state)
                {
                    case 0:
                    {
                        if (!taskAwaiter.IsCompleted)
                        {
                            state = 1;
                            taskAwaiter.OnCompleted(moveNext);
                        }
                        break;
                    }
                    case 1:
                    {
                        builder.SetResult(taskAwaiter.GetResult());
                        break;
                    }
                }
            };
            moveNext();
            return builder.Task;
        }
Ejemplo n.º 2
0
        /// <summary>Completes the method builder successfully.</summary>
        public void SetResult()
        {
            if (AsyncCausalityTracer.LoggingOn)
            {
                AsyncCausalityTracer.TraceOperationCompletion(this.Task, AsyncCausalityStatus.Completed);
            }

            // Mark the builder as completed.  As this is a void-returning method, this mostly
            // doesn't matter, but it can affect things like debug events related to finalization.
            _builder.SetResult();

            if (_synchronizationContext != null)
            {
                NotifySynchronizationContextOfCompletion();
            }
        }
 public void SetResult()
 {
     if (_useBuilder)
     {
         _methodBuilder.SetResult();
     }
     else
     {
         _haveResult = true;
     }
 }
Ejemplo n.º 4
0
 /// <summary>Marks the task as successfully completed.</summary>
 /// <param name="result">The result to use to complete the task.</param>
 public void SetResult(TResult result)
 {
     if (_useBuilder)
     {
         _methodBuilder.SetResult(result);
     }
     else
     {
         _result     = result;
         _haveResult = true;
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 ///     Marks the task as successfully completed.
 /// </summary>
 /// <exception cref="T:System.InvalidOperationException">The task has already completed.-or-The builder is not initialized.</exception>
 public void SetResult()
 {
     m_builder.SetResult(new VoidTaskResult());
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Completes the <see cref="T:System.Threading.Tasks.Task"/> in the
 ///             <see cref="T:System.Threading.Tasks.TaskStatus">RanToCompletion</see> state.
 ///
 /// </summary>
 /// <exception cref="T:System.InvalidOperationException">The builder is not initialized.</exception><exception cref="T:System.InvalidOperationException">The task has already completed.</exception>
 public void SetResult()
 {
     _builder.SetResult(_cachedCompleted);
 }
Ejemplo n.º 7
0
 public void Complete()
 {
     _methodBuilder.SetResult();
 }
 public void SetResult() => _builder.SetResult();
Ejemplo n.º 9
0
 /// <summary>
 /// Completes the <see cref="T:System.Threading.Tasks.Task"/> in the
 ///             <see cref="T:System.Threading.Tasks.TaskStatus">RanToCompletion</see> state.
 ///
 /// </summary>
 /// <exception cref="T:System.InvalidOperationException">The builder is not initialized.</exception><exception cref="T:System.InvalidOperationException">The task has already completed.</exception>
 public void SetResult()
 {
     builder.SetResult(CachedCompleted);
 }
        public static void RunAsyncMethodBuilderTests_NegativeTests()
        {
            // Incorrect usage for AsyncVoidMethodBuilder
            {
                var atmb = new AsyncTaskMethodBuilder();
                Assert.Throws<ArgumentNullException>(
                   () => { atmb.SetException(null); });
            }

            // Incorrect usage for AsyncTaskMethodBuilder
            {
                var avmb = AsyncVoidMethodBuilder.Create();
                Assert.Throws<ArgumentNullException>(
                 () => { avmb.SetException(null); });
            }

            // Creating a task builder, building it, completing it successfully, and making sure it can't be reset
            {
                var atmb = AsyncTaskMethodBuilder.Create();
                atmb.SetResult();
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetResult(); });
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetException(new Exception()); });
            }

            // Incorrect usage for AsyncTaskMethodBuilder<T>
            {
                var atmb = new AsyncTaskMethodBuilder<int>();
                Assert.Throws<ArgumentNullException>(
                   () => { atmb.SetException(null); });
            }

            // Creating a task builder <T>, building it, completing it successfully, and making sure it can't be reset
            {
                var atmb = AsyncTaskMethodBuilder<int>.Create();
                atmb.SetResult(43);
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetResult(44); });
                Assert.Throws<InvalidOperationException>(
                   () => { atmb.SetException(new Exception()); });
            }
        }
Ejemplo n.º 11
0
 public void SetResult()
 {
     // Accessing AsyncTaskMethodBuilder.s_cachedCompleted is faster than
     // accessing AsyncTaskMethodBuilder<T>.s_defaultResultTask.
     m_builder.SetResult(s_cachedCompleted);
 }
 /// <summary>
 /// Completes the <see cref="Threading.Tasks.Task"/> in the <see cref="TaskStatus.RanToCompletion"/> state.
 /// </summary>
 /// <exception cref="InvalidOperationException">The builder is not initialized.</exception>
 /// <exception cref="InvalidOperationException">The task has already completed.</exception>
 public void SetResult()
 {
     // Accessing AsyncTaskMethodBuilder._cachedCompleted is faster than accessing
     // AsyncTaskMethodBuilder<T>._defaultResultTask.
     _builder.SetResult(_cachedCompleted);
 }
 /// <summary>将任务标记为已成功完成。</summary>
 public void SetResult()
 {
     m_builder.SetResult(AsyncTaskMethodBuilder.s_cachedCompleted);
 }