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();
CancellationToken Struct
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.
CancellationTokenSource
then you can't cancel it. The token is an object that all the threads share, this object is set by theCancellationTokenSource.Cancel()
method. Once done so, theCancellationToken.IsCancellationRequested
would be true. Until then, it will always be false. (It cannot be set directly.) If you don't have aCancellationTokenSource
, then there is nothing that is capable of throwing the cancellation. You require aCancellationTokenSource
to cancel threads like that. – Der Kommissar Jun 16 '15 at 18:43