I start a task, that start other tasks and so forth.
Given that tree, if any task fails the result of the whole operation is useless. I'm considering using cancellation tokens. To my surprise, the token does not have a "CancelThisToken()" method...
So my question is: how can I, in possession of ONLY a CancellationToken, cancel it?
As the docs state you need to call the cancel method from the source object. Example code is included in the link you provided. Here are the relevant sections:
// Define the cancellation token.
CancellationTokenSource source = new CancellationTokenSource();
previouslyProvidedToken = source.Token;
...
source.Cancel();
how can I, in possession of ONLY a CancellationToken, cancel it?
Without a reference to the source you cannot cancel a token. That doesn't mean that you need the CancellationTokenSource
that first spawned the token. When given a CancellationToken
, you can create a new instance of token source assign it's token to the provided token and cancel it. All other parties that can read this token will see that it's cancellation has been requested.