Get Task CancellationToken

But what if my action is not lambda but a method placed in other class and I don’t have direct access to token? Is the only way is to pass token as state?

Yes, in that case, you would need to pass the token boxed as state, or included in some other type you use as state.

This is only required if you plan to use the CancellationToken within the method, however. For example, if you need to call token.ThrowIfCancellationRequested().

If you’re only using the token to prevent the method from being scheduled, then it’s not required.

Leave a Comment