So, task.Wait()
can be transformed to await task
. The semantics are different, of course, but this is roughly how I would go about transforming a blocking code with Waits
to an asynchronous code with awaits
.
My question is how to transform task.Wait(CancellationToken)
to the respective await
statement?
The CancellationTokenSource uses a ManualResetEvent internally and you can just wait for the exposed WaitHandle to pause the execution until it is set. var cts = new CancellationTokenSource(); Task. Run(() => { WaitHandle.
A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.
The CancellationToken is used in asynchronous task. The CancellationTokenSource token is used to signal that the Task should cancel itself. In the above case, the operation will just end when cancellation is requested via Cancel() method.
Wait(TimeSpan) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs: The task completes successfully. The task itself is canceled or throws an exception.
await
is used for asynchronous methods/delegates, which either accept a CancellationToken
and so you should pass one when you call it (i.e. await Task.Delay(1000, cancellationToken)
), or they don't and they can't really be canceled (e.g. waiting for an I/O result).
What you can do however, is abandon* these kinds of tasks with this extension method:
public static Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
return task.IsCompleted // fast-path optimization
? task
: task.ContinueWith(
completedTask => completedTask.GetAwaiter().GetResult(),
cancellationToken,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}
Usage:
await task.WithCancellation(cancellationToken);
* The abandoned task doesn't get cancelled, but your code behaves as though it has. It either ends with a result/exception or it will stay alive forever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With