문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top