Signals to a CancellationToken that it should be canceled.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|CancellationTokenSource()||Initializes a new instance of the class.|
|CancellationTokenSource(Int32)||Initializes a new instance of the class that will be canceled after the specified delay in milliseconds.|
|CancellationTokenSource(TimeSpan)||Initializes a new instance of the class that will be canceled after the specified time span.|
|Cancel()||Communicates a request for cancellation.|
|Cancel(Boolean)||Communicates a request for cancellation, and specifies whether remaining callbacks and cancelable operations should be processed.|
|CancelAfter(Int32)||Schedules a cancel operation on this after the specified number of milliseconds.|
|CancelAfter(TimeSpan)||Schedules a cancel operation on this after the specified time span.|
|CreateLinkedTokenSource(array<CancellationToken>)||Creates a that will be in the canceled state when any of the source tokens in the specified array are in the canceled state.|
|CreateLinkedTokenSource(CancellationToken, CancellationToken)||Creates a that will be in the canceled state when any of the source tokens are in the canceled state.|
|Dispose()||Releases all resources used by the current instance of the class.|
|Dispose(Boolean)||Releases the unmanaged resources used by the class and optionally releases the managed resources.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
For more information, see Cancellation in Managed Threads.
This type implements the IDisposable interface. When you have finished using an instance of the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.
The following example uses a random number generator to emulate a data collection application that reads 10 integral values from eleven different instruments. A value of zero indicates that the measurement has failed for one instrument, in which case the operation should be cancelled and no overall mean should be computed.
To handle the possible cancellation of the operation, the example instantiates a object that generates a cancellation token which is passed to a TaskFactory object. The TaskFactory object in turn passes the cancellation token to each of the tasks responsible for collecting readings for a particular instrument. The TaskFactory::ContinueWhenAll<TAntecedentResult, TResult>(array<Task<TAntecedentResult>>, Func<array<Task<TAntecedentResult>>, TResult>, CancellationToken) method is called to ensure that the mean is computed only after all readings have been gathered successfully. If a task has not because it has been cancelled, the call to the TaskFactory::ContinueWhenAll method throws an exception.
All public and protected members of are thread-safe and may be used concurrently from multiple threads, with the exception of Dispose, which must only be used when all other operations on the object have completed.