Tasks allow you to pass in a CancellationToken
. You can call ct.Cancel() in order to set the flag for cancellation. Within your actual execution logic, you can add in checks at certain "checkpoints" which test for if (ct.IsCancellationRequested)
and return the method from there or alternatively call ct.ThrowIfCancellationRequested()
.