コード例 #1
0
 public void SetException(Exception exception)
 {
     if (_source.HasTask)
     {
         _source.TrySetException(exception);
     }
     _exception = exception;
 }
コード例 #2
0
        private void Verify(ValueTaskCompletionSource <int> source, bool shouldFault)
        {
            var task = source.Task;

            Assert.False(task.IsCompleted);
            Assert.False(task.Status == TaskStatus.RanToCompletion);

            if (shouldFault)
            {
                Assert.True(source.TrySetException(new FormatException()));
                Assert.False(source.TrySetResult(42));
                Assert.False(source.TrySetException(new FormatException()));

                for (int i = 0; i < 2; i++) // can check multiple times
                {
                    Assert.True(task.IsFaulted);
                    Assert.True(task.IsCompleted);
                    Assert.False(task.Status == TaskStatus.RanToCompletion);
                    var ex = Assert.Throws <AggregateException>(() => task.Result);
                    Assert.IsType <FormatException>(ex.InnerException);
                }
            }
            else
            {
                Assert.True(source.TrySetResult(42));
                Assert.False(source.TrySetResult(42));
                Assert.False(source.TrySetException(new FormatException()));

                for (int i = 0; i < 2; i++) // can check multiple times
                {
                    Assert.False(task.IsFaulted);
                    Assert.True(task.IsCompleted);
                    Assert.True(task.Status == TaskStatus.RanToCompletion);
                    Assert.Equal(42, task.Result);
                }
            }

            Assert.False(source.IsNull); // still good
        }
コード例 #3
0
 public bool TrySetException(short token, Exception exception)
 {
     lock (this)
     {
         if (_isComplete)
         {
             return(false);
         }
         if (token != _version)
         {
             return(false);
         }
         _isComplete = true;
         if (_source.HasTask)
         {
             return(_source.TrySetException(exception));
         }
         _exception = exception;
         return(true);
     }
 }
コード例 #4
0
        /// <summary>
        /// Attempts to transition the underlying <see cref="ValueTask{TResult}"/> object
        /// into the <see cref="TaskStatus.Faulted"/> or <see cref="TaskStatus.Canceled"/> state,
        /// depending on the type of exception.
        /// </summary>
        /// <typeparam name="TResult">
        /// The type of the result value associated with the <see cref="ValueTaskCompletionSource{TResult}"/>.
        /// </typeparam>
        /// <param name="taskCompletionSource">The task completion source.</param>
        /// <param name="exception">The exception to bind to the <see cref="ValueTask{TResult}"/>.</param>
        /// <returns>True if the operation was succesful, false otherwise.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if any of <paramref name="taskCompletionSource"/> or <paramref name="exception"/> is <c>null</c>.
        /// </exception>
        public static bool TrySetExceptionOrCanceled <TResult>(
            this ValueTaskCompletionSource <TResult> taskCompletionSource,
            Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            if (exception is OperationCanceledException operationCanceledException)
            {
                var cancellationToken = operationCanceledException.CancellationToken;

                if (cancellationToken != default)
                {
                    return(taskCompletionSource.TrySetCanceled(cancellationToken));
                }

                return(taskCompletionSource.TrySetCanceled());
            }

            return(taskCompletionSource.TrySetException(exception));
        }
コード例 #5
0
 public void SetException(Exception exception)
 {
     _source.TrySetException(exception);
     _exception = exception;
 }