Reputation: 14525
Modern .NET APIs use CancellationTokenSource and CancellationToken for cancelling jobs across threads. Is there any reason to use these instead of the "old" AutoResetEvent and ManualResetEvent? I imagine that CancellationToken encapsulates something similar, as exposed by its WaitHandle property.
Upvotes: 3
Views: 3787
Reputation: 6716
Well yes. The CancellationTokenSource
uses a ManualResetEvent
internally to handle the reporting of the cancellation event.
Still you should prefer to use the CancellationTokenSource
for cancelling stuff for a couple of reasons:
CancellationTokenSource
. Especially many things in System.Threading
(and sub-packages)ManualResetEvent
the CancellationTokenSource
does a couple of things to optimize things internally. Lazy initialization and such things. I hope that makes it just a little faster and run with less overhead under some conditions.Upvotes: 6