CancellationTokenSource
Context: CancellationTokenSource is used to create a CancellationToken and to request cancellation by calling Cancel().
using System;using System.Threading;using System.Threading.Tasks;
public class TimeoutExample{ public static async Task Main() { using var cts = new CancellationTokenSource(); cts.CancelAfter(5000); // auto‑cancel after 5 seconds
// Request cancellation manually (e.g., user press) // cts.Cancel();
try { await LongRunningOperationAsync(cts.Token); } catch (OperationCanceledException) { Console.WriteLine("Operation cancelled"); } }
static async Task LongRunningOperationAsync(CancellationToken token) { for (int i = 0; i < 100; i++) { token.ThrowIfCancellationRequested(); await Task.Delay(100); } }}Obtaining a token
Section titled “Obtaining a token”CancellationToken token = cts.Token;Real-world usage example
Section titled “Real-world usage example”Timeout for HTTP requests: Use CancellationTokenSource with CancelAfter to set a timeout.
Example: HttpClient with cancellation