Another important function of the TPL is to equip developers with ready-made data structures to cancel running tasks. Those of you that have a classic threading background will be aware of how difficult it used to be to make threads support canceling with all the custom home-grown logic, but this is no longer the case. The .NET Framework provides two classes to support task cancellation:
- CancellationTokenSource: This class is responsible for creating cancellation tokens and passing the cancellation request to all the tokens that were created via the source
- CancellationToken: This class is used by listeners to monitor the current state of a request
To create tasks that can be canceled, we need to perform the following steps:
- Create an instance of the System.Threading.CancellationTokenSource class, which further provides a System.Threading.CancellationToken...