Canceling managed threads
Canceling asynchronous work in .NET is based on the use of a cancellation token. A token is a simple object that is used to signal that a cancellation request has been made to another thread. The CancellationTokenSource
object manages these requests and contains a token. If you want to cancel several operations with the same trigger, the same token should be provided to all of the threads to be canceled.
A CancellationTokenSource
instance has a Token
property to access the CancellationToken
property and pass it to one or more asynchronous operations. The request to cancel can only be made from the CancellationTokenSource
object. The CancellationToken
property provided to the other operations receives the signal to cancel but cannot initiate a cancellation.
CancellationTokenSource
implements the IDisposable
interface, so be sure to call Dispose
when you are freeing your managed resources. A using
statement or block to automatically dispose of the token...