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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.