qckmini6
qckmini6

Reputation: 124

CancellationTokenSource always has the token canceled - C#

So the IsCancellationRequested property of the CancellationToken is always true, even after declaring it as a variable and checking the token whether it was cancelled or not directly and it's infuriating me

how can I run a task if the token is already canceled?

Upvotes: 1

Views: 600

Answers (1)

TheGeneral
TheGeneral

Reputation: 81483

The IsCancellationRequested will only be true if cancellation has been requested (public or not)

Gets whether cancellation has been requested for this CancellationTokenSource.

Remarks

This property indicates whether cancellation has been requested for this token source, such as due to a call to its Cancel method.

If this property returns true, it only guarantees that cancellation has been requested. It does not guarantee that every handler registered with the corresponding token has finished executing, nor that cancellation requests have finished propagating to all registered handlers. Additional synchronization may be required, particularly in situations where related objects are being canceled concurrently.

If it is public and its true (and this is not expected) i would track down all the code using it and try to work out who cancelled it and why, and if this fits in with your desired architecture

Upvotes: 1

Related Questions