ThrowIfCancellationRequested
Context: ThrowIfCancellationRequested checks if the token has been cancelled and, if so, throws an OperationCanceledException.
using System.Threading;
public class Example{ public void Work(CancellationToken token) { token.ThrowIfCancellationRequested(); // throws if cancelled // More work... }}Manual checking
Section titled “Manual checking”if (token.IsCancellationRequested){ // cleanup before throwing token.ThrowIfCancellationRequested();}Real-world usage example
Section titled “Real-world usage example”Responsive UI: Cancel a long‑running operation when the user clicks a “Cancel” button.
Example: Cancellation in managed threads