public AwaitableCancellationToken(CancellationToken cancellationToken)
 {
     if (cancellationToken.IsCancellationRequested)
     {
         _task         = TaskConstants <T> .Canceled;
         _registration = default;
     }
     else if (cancellationToken.CanBeCanceled)
     {
         var promise = new Promise <T>();
         _task         = promise.Task;
         _registration = promise.AssignCancellationToken(cancellationToken);
     }
     else
     {
         _task         = TaskConstants <T> .Infinite;
         _registration = default;
     }
 }
Esempio n. 2
0
        private static async Task <T> WithCancellationAsync <T>(Task <T> task, CancellationToken cancellationToken, Action <Task <T> > disposeAction = null)
        {
            var cancellationPromise = new Promise <T>();

            using (cancellationPromise.AssignCancellationToken(cancellationToken))
            {
                var completedTask = await Task.WhenAny(task, cancellationPromise.Task).ConfigureAwait(false);

                if (completedTask != task)
                {
                    if (disposeAction != null)
                    {
                        task.ContinueWithSynchronously(disposeAction, CancellationToken.None).IgnoreAwait(Log);
                    }
                    else
                    {
                        task.IgnoreAwait(Log);
                    }
                }
                return(completedTask.GetResult());
            }
        }
Esempio n. 3
0
 public void AssignCancellationToken(CancellationToken cancellationToken) =>
 _inner.AssignCancellationToken(cancellationToken);
Esempio n. 4
0
 public CancellationTokenRegistration AssignCancellationToken(CancellationToken cancellationToken) =>
 _inner.AssignCancellationToken(cancellationToken);