How to Pass CancellationToken.None When Work Must Not Be Canceled in C#
CancellationToken.None is a token that can never be canceled. Pass it on purpose to work that has to finish even after the caller cancels, such as cleaning up after a canceled download, so the cancellation that stopped the download can't stop the clean-up too.
The reference for CancellationToken.None: Returns an empty CancellationToken value. Its remarks say what empty means: The cancellation token returned by this property cannot be canceled; that is, its CanBeCanceled property is false.
Clean-up is part of canceling. Microsoft's guide to cancellation: The cancellation request means that the operation should stop as soon as possible after any required cleanup is performed. The clean-up can't use the caller's token, though, because a canceled token stays canceled: After the IsCancellationRequested property of the token has been set to true, it cannot be reset to false. CancellationToken.None gives the clean-up a token that nothing can cancel.
Syntax
try
{
await <WorkAsync>(<cancellationToken>);
}
catch (OperationCanceledException)
{
await <CleanUpAsync>(CancellationToken.None);
throw;
}
| Part | What it is |
|---|---|
<WorkAsync>(<cancellationToken>) | The work that should stop when the caller cancels, given the caller's token |
catch (OperationCanceledException) | Runs when the work stopped because its token was canceled |
<CleanUpAsync>(CancellationToken.None) | Work that has to finish anyway, such as deleting what the canceled work left behind. CancellationToken.None goes where the method takes a token |
throw; | Passes the cancellation on, so the caller knows the work didn't finish |
The Example
A console app called CancellationTokenNone downloads a photo in three parts of 300 milliseconds each, and the user gives up after 500 milliseconds. The download stops, and the part already saved is deleted with CancellationToken.None, so that clean-up finishes even though the download was canceled. Short waits stand in for the real downloading and deleting.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n CancellationTokenNone
cd CancellationTokenNone
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/CancellationTokenNone/CancellationTokenNone.csproj:
Determining projects to restore...
Restored /Users/you/source/CancellationTokenNone/CancellationTokenNone.csproj (in 45 ms).
Restore succeeded.
dotnet new console -n CancellationTokenNone creates a folder named CancellationTokenNone with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or CancellationTokenNone.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the code at the top of the file:
// The user gives up on a download after half a second.
using CancellationTokenSource source = new CancellationTokenSource();
source.CancelAfter(500);
try
{
await Downloader.DownloadAsync("photo.jpg", source.Token);
Console.WriteLine("Download finished");
}
catch (OperationCanceledException e)
{
Console.WriteLine($"Download canceled: {e.GetType().Name}: {e.Message}");
}
A CancellationTokenSource makes a token and cancels it. source.CancelAfter(500), in its reference's words, Schedules a cancel operation on this CancellationTokenSource after the specified number of milliseconds. source.Token goes to DownloadAsync, and the catch prints the exception's type and message when the download is canceled. using disposes the source when the code at the top of the file finishes, as its reference asks: When you've finished using the type, you should dispose of it either directly (by calling its Dispose method) or indirectly (with a language construct such as using in C#).
Then the download:
static class Downloader
{
public static async Task DownloadAsync(string name, CancellationToken cancellationToken)
{
try
{
for (int part = 1; part <= 3; part++)
{
// Each part takes 300 milliseconds. A canceled token ends the wait early.
await Task.Delay(300, cancellationToken);
Console.WriteLine($"Part {part} of {name} saved");
}
}
catch (OperationCanceledException)
{
// The saved parts must be deleted even though the download was canceled,
// so the clean-up gets a token that never cancels.
await DeletePartsAsync(name, CancellationToken.None);
throw;
}
}
Each part waits 300 milliseconds with Task.Delay(300, cancellationToken). When the token is canceled during a wait, Task.Delay stops early and throws a TaskCanceledException, which catch (OperationCanceledException) catches. The catch block deletes the saved parts, with CancellationToken.None, and throw; passes the cancellation on.
Last, the clean-up:
private static async Task DeletePartsAsync(string name, CancellationToken cancellationToken)
{
// Deleting takes 100 milliseconds.
await Task.Delay(100, cancellationToken);
Console.WriteLine($"Saved parts of {name} deleted");
}
}
DeletePartsAsync takes a CancellationToken, like the real async methods it stands for, and its caller decides which token to pass. It waits on that token for 100 milliseconds, so a canceled token would stop it too.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Part 1 of photo.jpg saved
Saved parts of photo.jpg deleted
Download canceled: TaskCanceledException: A task was canceled.

Part 1 was saved. The token was canceled during the wait for part 2, so Task.Delay threw a TaskCanceledException and part 3 never started. The catch block still deleted what was saved, because DeletePartsAsync got CancellationToken.None. Then throw; passed the cancellation on, and the top of the file printed Download canceled with the exception's type and message.
The Complete File

// The user gives up on a download after half a second.
using CancellationTokenSource source = new CancellationTokenSource();
source.CancelAfter(500);
try
{
await Downloader.DownloadAsync("photo.jpg", source.Token);
Console.WriteLine("Download finished");
}
catch (OperationCanceledException e)
{
Console.WriteLine($"Download canceled: {e.GetType().Name}: {e.Message}");
}
static class Downloader
{
public static async Task DownloadAsync(string name, CancellationToken cancellationToken)
{
try
{
for (int part = 1; part <= 3; part++)
{
// Each part takes 300 milliseconds. A canceled token ends the wait early.
await Task.Delay(300, cancellationToken);
Console.WriteLine($"Part {part} of {name} saved");
}
}
catch (OperationCanceledException)
{
// The saved parts must be deleted even though the download was canceled,
// so the clean-up gets a token that never cancels.
await DeletePartsAsync(name, CancellationToken.None);
throw;
}
}
private static async Task DeletePartsAsync(string name, CancellationToken cancellationToken)
{
// Deleting takes 100 milliseconds.
await Task.Delay(100, cancellationToken);
Console.WriteLine($"Saved parts of {name} deleted");
}
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Passing the caller's token to the clean-up | With DeletePartsAsync(name, cancellationToken) in the catch block, the run printed Part 1 of photo.jpg saved and then Download canceled: TaskCanceledException: A task was canceled., with no Saved parts of photo.jpg deleted line. The token was already canceled, so the clean-up's wait threw too | Pass CancellationToken.None to work that has to finish after a cancellation, as the example does |
Passing CancellationToken.None to the work that should stop | With Task.Delay(300, CancellationToken.None) in the loop, all three parts were saved and the run ended with Download finished, even though the user gave up after 500 milliseconds | Give the work the caller's token, and CancellationToken.None only to the work that must not stop |
Leaving out throw; after the clean-up | The run printed Saved parts of photo.jpg deleted and then Download finished. The caller never learned that the download was canceled | End the catch block with throw;, so the cancellation reaches the caller after the clean-up |
FAQ
Is CancellationToken.None the same as default?
Yes. The reference: You can also use the C# default(CancellationToken) statement to create an empty cancellation token. And: Two empty cancellation tokens are always equal. A method can declare its token as CancellationToken cancellationToken = default, so that callers may leave it out, and then it gets an empty token. This program checks both, next to a token from a CancellationTokenSource:
using CancellationTokenSource source = new CancellationTokenSource();
Console.WriteLine($"None can be canceled: {CancellationToken.None.CanBeCanceled}");
Console.WriteLine($"A source's token can be canceled: {source.Token.CanBeCanceled}");
Console.WriteLine($"default equals None: {default(CancellationToken) == CancellationToken.None}");
Describe();
// A method that its callers may call without a token.
static void Describe(CancellationToken cancellationToken = default)
{
Console.WriteLine($"Called without a token, it can be canceled: {cancellationToken.CanBeCanceled}");
}
None can be canceled: False
A source's token can be canceled: True
default equals None: True
Called without a token, it can be canceled: False
Why write CancellationToken.None and not leave the token out?
Because a missing token looks like a token someone forgot. Code analysis rule CA2016 looks for exactly that: This rule locates method invocations that could accept a CancellationToken parameter, but are not passing any, and suggests to forward the parent method's CancellationToken to them. Writing CancellationToken.None shows that the work isn't meant to stop, and the rule accepts it: You can also explicitly pass default in C# (Nothing in Visual Basic) or None to suppress the rule violation.
Conclusion
CancellationToken.None is a token that can never be canceled. Give the caller's token to work that should stop when the caller cancels, and CancellationToken.None to work that has to finish anyway, such as the clean-up in a catch (OperationCanceledException) block. Then rethrow with throw;, so the caller still learns about the cancellation.
Additional Resources
CancellationToken.None— the empty token, and how it compares withdefault.- Cancellation in managed threads — how tokens, sources and clean-up work together.
CancellationTokenSource.CancelAfter— cancel a token after a delay.CancellationTokenSource— the object that makes and cancels tokens, and why to dispose it.- CA2016: Forward the CancellationToken parameter to methods that take one — when to pass the caller's token, and when to pass
CancellationToken.None.
