Search code examples
c#taskasync-awaitasync-ctpcancellation-token

CancellationToken UnRegister Action


I have a token for various tasks and I need to better manage their cancellation, to be notified of a cancellation I can use:

token.Register(RegisterMethod);

How can I remove this "subscription"? Is there any way to "UnRegister"?

I thought about doing a workaround with TaskCompletionSource. But I do not know if it would work well. What is the best way to solve this approach?


Solution

  • CancellationToken.Register returns a CancellationTokenRegistration instance. If you call Dispose on that instance, your registration will be removed.