CancellationToken in C# simplifies managing long-running jobs but has important limitations. Key lessons: tokens cannot be persisted outside memory, different libraries implement cancellation differently (Parallel.ForEachAsync immediately kills running tasks), and tokens don't automatically handle server restarts or deployments. While CancellationToken provides a solid foundation for graceful job cancellation, applications must be built to handle its boundaries and quirks, particularly around persistence, library-specific behaviors, and infrastructure-level shutdowns.

4m read timeFrom spin.atomicobject.com
Post cover image
Table of contents
Don’t Try to Persist the Token Out of MemoryDifferent Cancellation Implementation Across LibrariesThe Limit of CancellationToken’s Authority
2 Comments

Sort: