示例#1
0
        public static ValueTask <TResult> FromException <TResult>(Exception exception)
        {
#if NET50 || LESSTHAN_NET45
            return(ValueTask.FromException <TResult>(exception));
#elif GREATERTHAN_NET45 || GREATERTHAN_NETSTANDARD12 || TARGETS_NETCORE
            return(new ValueTask <TResult>(Task.FromException <TResult>(exception)));
#else
            return(new ValueTask <TResult>(TaskExEx.FromException <TResult>(exception)));
#endif
        }
示例#2
0
        public static ValueTask <TResult> FromCanceled <TResult>(CancellationToken cancellationToken)
        {
#if NET50 || LESSTHAN_NET45
            return(ValueTask.FromCanceled <TResult>(cancellationToken));
#elif GREATERTHAN_NET45 || GREATERTHAN_NETSTANDARD12 || TARGETS_NETCORE
            return(new ValueTask <TResult>(Task.FromCanceled <TResult>(cancellationToken)));
#else
            return(new ValueTask <TResult>(TaskExEx.FromCanceled <TResult>(cancellationToken)));
#endif
        }
示例#3
0
        /// <summary>
        ///     Creates a Task that will complete after a time delay.
        /// </summary>
        /// <param name="millisecondsDelay">The number of milliseconds to wait before completing the returned Task</param>
        /// <param name="cancellationToken">The cancellation token that will be checked prior to completing the returned Task</param>
        /// <returns>A Task that represents the time delay</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     The <paramref name="millisecondsDelay" /> is less than -1.
        /// </exception>
        /// <exception cref="ObjectDisposedException">
        ///     The provided <paramref name="cancellationToken" /> has already been disposed.
        /// </exception>
        /// <remarks>
        ///     If the cancellation token is signaled before the specified time delay, then the Task is completed in
        ///     Canceled state.  Otherwise, the Task is completed in RanToCompletion state once the specified time
        ///     delay has expired.
        /// </remarks>
        public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken)
        {
            // Throw on non-sensical time
            if (millisecondsDelay < -1)
            {
                throw new ArgumentOutOfRangeException(nameof(millisecondsDelay), "The value needs to be either -1 (signifying an infinite timeout), 0 or a positive integer.");
            }

            Contract.EndContractBlock();
            // some short-cuts in case quick completion is in order
            if (cancellationToken.IsCancellationRequested)
            {
                // return a Task created as already-Canceled
                return(TaskExEx.FromCanceled(cancellationToken));
            }

            if (millisecondsDelay == 0)
            {
                // return a Task created as already-RanToCompletion
                return(TaskExEx.CompletedTask);
            }

            var source = new TaskCompletionSource <bool>();

            if (cancellationToken.CanBeCanceled)
            {
                source.Task.AssignCancellationToken(cancellationToken, null, null);
            }

            var timeout = RootedTimeout.Launch
                          (
                () => source.TrySetResult(true),
                () => source.TrySetCanceled(),
                millisecondsDelay,
                cancellationToken
                          );

            source.Task.SetPromiseCheck(() => timeout.CheckRemaining());

            return(source.Task);
        }
示例#4
0
 public static Task <TResult> FromResult <TResult>(TResult result)
 {
     return(TaskExEx.FromResult(result));
 }
示例#5
0
 /// <summary>Creates a <see cref="ValueTask{TResult}"/> that has completed with the specified exception.</summary>
 /// <param name="exception">The exception with which to complete the task.</param>
 /// <returns>The faulted task.</returns>
 public static ValueTask <TResult> FromException <TResult>(Exception exception)
 {
     return(new ValueTask <TResult>(TaskExEx.FromException <TResult>(exception)));
 }
示例#6
0
 /// <summary>Creates a <see cref="ValueTask"/> that has completed with the specified exception.</summary>
 /// <param name="exception">The exception with which to complete the task.</param>
 /// <returns>The faulted task.</returns>
 public static ValueTask FromException(Exception exception)
 {
     return(new ValueTask(TaskExEx.FromException(exception)));
 }
示例#7
0
 /// <summary>Creates a <see cref="ValueTask{TResult}"/> that has completed due to cancellation with the specified cancellation token.</summary>
 /// <param name="cancellationToken">The cancellation token with which to complete the task.</param>
 /// <returns>The canceled task.</returns>
 public static ValueTask <TResult> FromCanceled <TResult>(CancellationToken cancellationToken)
 {
     return(new ValueTask <TResult>(TaskExEx.FromCanceled <TResult>(cancellationToken)));
 }
示例#8
0
 /// <summary>Creates a <see cref="ValueTask"/> that has completed due to cancellation with the specified cancellation token.</summary>
 /// <param name="cancellationToken">The cancellation token with which to complete the task.</param>
 /// <returns>The canceled task.</returns>
 public static ValueTask FromCanceled(CancellationToken cancellationToken)
 {
     return(new ValueTask(TaskExEx.FromCanceled(cancellationToken)));
 }
示例#9
0
 public static Task <TResult> FromException <TResult>(Exception exception)
 {
     return(TaskExEx.FromException <TResult>(exception));
 }
示例#10
0
 public static Task FromException(Exception exception)
 {
     return(TaskExEx.FromException(exception));
 }
示例#11
0
 public static Task FromCanceled(CancellationToken cancellationToken)
 {
     return(TaskExEx.FromCanceled(cancellationToken));
 }