Question

The documentation on CancellationToken.CanBeCanceled describes what it does, but doesn't mention when it can ever be false. Hence it's not entirely clear to me when I should pay attention to this property.

In which circumstances can it be false and when should I look at it?

Was it helpful?

Solution

Well, CancellationToken.None returns one that isn't cancellable.

If you're the user of an API that insists on receiving a CancellationToken, but you've got no interest in ever attempting to cancel the work, then you could just pass it this token.

As someone building an API, you may be able to make certain optimizations if CanBeCancelled returns false - maybe it's "expensive" for you to check the IsCancellationRequested property.

OTHER TIPS

For example, when "whole" code of async method is the calling one method on remote machine (server for example) which can not be canceled (the remote machine does not provide any way, how to cancel that). Then the task can not be canceled -> task can be only completed (or throw exception).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top