CancellationTokenSource can just be cancelled instead of disposing?

34 Views Asked by At

I'm working on some code that uses fire and forget tasks, which can also be cancelled.

I always thought you had to Cancel AND dispose of CancellationTokenSources, but reading here https://blog.stephencleary.com/2022/03/cancellation-2-requesting-cancellation.html, they suggest you only need to do one or the other? As Cancel() will also dispose of the timer and any registrations.

So in the case of a fire and forget task, is it OK to just call Cancel()? And not have to worry about trying to call dispose after the task completes?

0

There are 0 best solutions below