Can I get CancellationToken
which was passed to Task
constructor during task action executing. Most of samples look like this:
Cancella
As other answers state, you can pass the token as a parameter to your method. However, it's important to remember that you still want to pass it to the Task
as well. Task.Factory.StartNew( () => YourMethod(token), token)
, for example.
This insures that:
The Task
will not run if cancellation occurs before the Task
executes (this is a nice optimization)
An OperationCanceledException
thrown by the called method correctly transitions the Task to a Canceled
state