If I have a CancellationTokenSource that is still in scope when I’m checking for cancellation — e.g., if I’ve just made a database query and have not yet passed the CancellationToken down to Tasks to process the results — should I access IsCancellationRequested from the source or from its token?
In other words, if both options are available, which is preferred, and why?
1:
myCancellationTokenSource.IsCancellationRequested
2:
myCancellationTokenSource.Token.IsCancellationRequested
In this particular scenario, I believe the two are essentially equivalent. I would prefer using the Token if only because this simplifies refactoring if you later split off the logic checking cancellation from the logic creating the cancellation source. To further that end, I would store the token in a local reference and use that reference for the checking.