Cancellation

TAP methods often provide an overload with a CancellationToken parameter that you can use to cancel the ongoing operation. Such an overload only exists if the underlying operation has a meaningful way to abort work. If present, the CancellationToken parameter is passed after the parameters that occur on the corresponding synchronous variant.

For example, if an operation gets asynchronous behavior because it’s processed through a work queue, cancellation can be achieved by deleting the item from the queue if it hasn’t started yet. Another typical example is when the asynchronous operation performs other asynchronous operations which are cancelable. In that case, the CancellationToken is passed down to the underlying operations, providing ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.