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?
await
is used for asynchronous methods/delegates, which either accept aCancellationToken
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:
Usage:
* 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.
To create a new
Task
that represents an existing task but with an additional cancellation token is quite straightforward. You only need to callContinueWith
on the task, use the new token, and propagate the result/exceptions in the body of the continuation.This allows you to write
task.WithCancellation(cancellationToken)
to add a token to a task, which you can thenawait
.