简体   繁体   中英

Can I use CancellationTokenSource.Cancel and CancellationTokenSource.CancelAfter(timeSpan) for the same task?

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM