Provides a cancellation token in .NET for cooperative cancellation of asynchronous or long-running synchronous operations.
Provides a cancellation token in NET. Starting with .NET Framework 4.0, the .NET Framework uses a unified model for cooperative cancellation of asynchronous or long-running synchronous operations that involves two objects:
- A
CancellationTokenSourceobject, which provides a cancellation token through itsTokenproperty and sends a cancellation message by calling itsCancelorCancelAftermethod. - A
CancellationTokenobject, which indicates whether cancellation is requested.
A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.Token property. You then pass the cancellation token to any number of threads, tasks, or operations that should receive notice of cancellation. The token cannot be used to initiate cancellation. When the owning object calls CancellationTokenSource.Cancel, the IsCancellationRequested property on every copy of the cancellation token is set to true. The objects that receive the notification can respond in whatever manner is appropriate.