Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can CancellationToken.CanBeCanceled be false?

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?

like image 594
Roman Starkov Avatar asked Nov 22 '10 13:11

Roman Starkov


1 Answers

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.

like image 59
Damien_The_Unbeliever Avatar answered Sep 18 '22 15:09

Damien_The_Unbeliever