Microsoft created a standardized cancellation implementation that has far-reaching capabilities beyond its original use case. Sometimes canceling is a good thing. In many of my .NET projects, I have had plenty of motivation to cancel both internal and external processes. Microsoft learned that developers were approaching this common use case in a variety of complex implementations and decided there must be a better way. By Davit Asryan.
CancellationToken was introduced in .NET 4 as a means to enhance and standardize the existing solutions for canceling operations. There are some general approaches to handling cancellation that popular programming languages tend to implement. Details in the article:
- CancellationToken Under a Microscope
- CancellationTokens for Advanced Events
- CancellationToken for Timeout
- CancellationToken As a Notification Mechanism
- An Expansive CancellationToken Toolbox
A common cancellation communication pattern was introduced as CancellationToken, which was built using lower-level multithreading and interprocess communication constructs. As part of author’s initial research into this pattern—and after having dug through the actual .NET source code for Microsoft’s implementation—I found that CancellationToken can solve a much broader set of problems: subscriptions on applications’ run states, timing out operations using different triggers, and general interprocess communications via flags. Good read!
[Read More]