Home > Enterprise >  Can I use CancellationTokenSource.Cancel and CancellationTokenSource.CancelAfter(timeSpan) for the s
Can I use CancellationTokenSource.Cancel and CancellationTokenSource.CancelAfter(timeSpan) for the s

Time:04-06

I didn't see this in the docs anywhere -

If I want to use CancellationTokenSource.CancelAfter(600000) as a failsafe in case my CancellationTokenSource.Cancel() call never gets called.

Will the CancelAfter() interfere with the Cancel() call in any way?

CodePudding user response:

Nope, no interference. When a CancellationTokenSource is canceled, it's an atomic operation. Either the Cancel will occur first and will dispose immediately the active CancelAfter-related timer, or the timer will be triggered first and the subsequent Cancel will be a no-op.

If you want you can study the source code of the Cancel method here.

  • Related