Beispiel #1
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
        }
Beispiel #2
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);
        }
Beispiel #3
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)));
 }
Beispiel #4
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)));
 }
Beispiel #5
0
 public static Task FromCanceled(CancellationToken cancellationToken)
 {
     return(TaskExEx.FromCanceled(cancellationToken));
 }