Пример #1
0
 public static Task FromCanceled(CancellationToken cancellationToken)
 {
     return(TaskEx.FromCanceled(cancellationToken));
 }
Пример #2
0
 public static Task <TResult> FromCanceled <TResult>(CancellationToken cancellationToken)
 {
     return(TaskEx.FromCanceled <TResult>(cancellationToken));
 }
Пример #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="T:System.ArgumentOutOfRangeException">
        ///     The <paramref name="millisecondsDelay" /> is less than -1.
        /// </exception>
        /// <exception cref="T:System.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(TaskEx.FromCanceled(cancellationToken));
            }

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

            var source = new TaskCompletionSource <bool>();

            if (millisecondsDelay > 0)
            {
                var timeout = RootedTimeout.Launch
                              (
                    () =>
                {
                    try
                    {
                        source.SetResult(true);
                    }
                    catch (InvalidOperationException exception)
                    {
                        // Already canceled
                        No.Op(exception);
                    }
                },
                    millisecondsDelay,
                    cancellationToken
                              );
                source.Task.SetPromiseCheck(() => timeout.CheckRemaining());
            }

            if (cancellationToken.CanBeCanceled)
            {
                cancellationToken.Register
                (
                    () =>
                {
                    try
                    {
                        source.SetCanceled();
                    }
                    catch (InvalidOperationException exception)
                    {
                        // Already timed out
                        No.Op(exception);
                    }
                }
                );
            }

            return(source.Task);
        }