CancellationTokenSource.Cancel Method ()
Communicates a request for cancellation.
Assembly: mscorlib (in mscorlib.dll)
Any callbacks or cancelable operations registered with the CancellationToken will be executed.
Cancelable operations and callbacks registered with the token should not throw exceptions.
However, this overload of Cancel will aggregate any exceptions thrown into an AggregateException, such that one callback throwing an exception will not prevent other registered callbacks from being executed.
The ExecutionContext that was captured when each callback was registered will be reestablished when the callback is invoked.
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 CancellationTokenSource 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>(Task<'TAntecedentResult>, Func<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<'TAntecedentResult, 'TResult> method throws an exception.
Available since 8
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Available since 8.1