In modern .NET applications, asynchronous programming is essential—but with it comes the risk of runaway tasks, unresponsive UIs, or server overloads.
Enter CancellationToken: a smart way to inject cancellation logic into your async workflows.
🔹 External signal stops task execution gracefully
🔹 Check IsCancellationRequested inside the task
🔹 Combine multiple cancellation sources
🔹 Cancel API calls when the user navigates away
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
using CancellationTokenSource cts = new CancellationTokenSource();
Console.WriteLine("Press any key to cancel...");
Task.Run(() =>
{
Console.ReadKey();
cts.Cancel();
});
try
{
await FetchDataAsync(cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation was cancelled.");
}
}
static async Task FetchDataAsync(CancellationToken token)
{
using HttpClient client = new HttpClient();
Console.WriteLine("Fetching data...");
HttpResponseMessage response = await client.GetAsync(
"https://jsonplaceholder.typicode.com/posts", token
);
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync(token);
Console.WriteLine("Data fetched successfully.");
}
}
Using CancellationToken not only protects your application from wasteful processing, it also keeps user experience snappy and systems resilient.
🔵 Follow for .NET tips: Connect with me on LinkedIn