The CancellationToken is a .NET struct that enables cooperative cancellation of synchronous or asynchronous methods.
Questions tagged [cancellation-token]
366 questions
229
votes
4 answers
Cancellation token in Task constructor: why?
Certain System.Threading.Tasks.Task constructors take a CancellationToken as a parameter:
CancellationTokenSource source = new CancellationTokenSource();
Task t = new Task (/* method */, source.Token);
What baffles me about this is that there is no…

Colin
- 2,693
- 4
- 20
- 14
210
votes
7 answers
When to dispose CancellationTokenSource?
The class CancellationTokenSource is disposable. A quick look in Reflector proves usage of KernelEvent, a (very likely) unmanaged resource.
Since CancellationTokenSource has no finalizer, if we do not dispose it, the GC won't do it.
On the other…

George Mamaladze
- 7,593
- 2
- 36
- 52
172
votes
5 answers
Why CancellationToken is separate from CancellationTokenSource?
I'm looking for a rationale of why .NET CancellationToken struct was introduced in addition to CancellationTokenSource class. I understand how the API is to be used, but want to also understand why it is designed that way.
I.e., why do we have:
var…

Andrey Tarantsov
- 8,965
- 7
- 54
- 58
166
votes
4 answers
How to use the CancellationToken without throwing/catching an exception?
Compared to the preceding code for class RulyCanceler, I wanted to run code using CancellationTokenSource.
How do I use it as mentioned in Cancellation Tokens, i.e. without throwing/catching an exception? Can I use the IsCancellationRequested…

Fulproof
- 4,466
- 6
- 30
- 50
163
votes
5 answers
Default parameter for CancellationToken
I have some async code that I would like to add a CancellationToken to. However, there are many implementations where this is not needed so I would like to have a default parameter - perhaps CancellationToken.None. However,
Task DoStuff(....,…

tofutim
- 22,664
- 20
- 87
- 148
76
votes
3 answers
How to reset a CancellationToken properly?
I have been playing round with the Async CTP this morning and have a simple program with a button and a label. Click the button and it starts updating the label, stop the button it stops writing to the label. However, I'm not sure how to reset the…

poco
- 2,935
- 6
- 37
- 54
76
votes
4 answers
Linking Cancellation Tokens
I use a cancellation token that is passed around so that my service can be shut down cleanly. The service has logic that keeps trying to connect to other services, so the token is a good way to break out of these retry loops running in separate…

Retrocoder
- 4,483
- 11
- 46
- 72
66
votes
4 answers
Should I always add CancellationToken to my controller actions?
Is this a good practice to always add CancellationToken in my actions no matter if operation is long or not?
I'm currently adding it to every action and I don't know if it's right or wrong.
[ApiController]
[Route("api/[controller]")]
public class…

Konrad
- 6,385
- 12
- 53
- 96
62
votes
4 answers
CancellationToken with async Dapper methods?
I'm using Dapper 1.31 from Nuget. I have this very simple code snippet,
string connString = "";
string query = "";
int val = 0;
CancellationTokenSource tokenSource = new CancellationTokenSource();
using (IDbConnection conn = new…

Pedigree
- 2,384
- 3
- 23
- 28
61
votes
4 answers
Using CancellationToken for timeout in Task.Run does not work
OK, my questions is really simple. Why this code does not throw TaskCancelledException?
static void Main()
{
var v = Task.Run(() =>
{
Thread.Sleep(1000);
return 10;
}, new CancellationTokenSource(500).Token).Result;
…

Aliostad
- 80,612
- 21
- 160
- 208
57
votes
3 answers
Should we use CancellationToken with MVC/Web API controllers?
There are different examples for async controllers. Some of them use CancellationToken in method definition:
public async Task ShowItem(int id, CancellationToken cancellationToken)
{
await Database.GetItem(id, cancellationToken);
…

user1224129
- 2,759
- 3
- 27
- 29
47
votes
8 answers
How to make a method cancelable without it becoming ugly?
I am currently in the process of retrofitting our long-running methods to be cancelable. I am planning on using System.Threading.Tasks.CancellationToken to implement that.
Our methods generally perform a few long-running steps (sending commands to…

Jens
- 25,229
- 9
- 75
- 117
46
votes
6 answers
NetworkStream.ReadAsync with a cancellation token never cancels
Here the proof.
Any idea what is wrong in this code ?
[TestMethod]
public void TestTest()
{
var tcp = new TcpClient() { ReceiveTimeout = 5000, SendTimeout = 20000 };
tcp.Connect(IPAddress.Parse("176.31.100.115"), 25);
…

Softlion
- 12,281
- 11
- 58
- 88
43
votes
5 answers
Is default(CancellationToken) equivalent to CancellationToken.None?
Looking at the implementation of CancellationToken.None, it is simply returning default(CancellationToken). However, I see no reference in CancellationToken's documentation that the two are equivalent.
I'd like to offer an API like this but not…

Cory Nelson
- 29,236
- 5
- 72
- 110
40
votes
3 answers
CancellationToken UnRegister Action
I have a token for various tasks and I need to better manage their cancellation, to be notified of a cancellation I can use:
token.Register(RegisterMethod);
How can I remove this "subscription"?
Is there any way to "UnRegister"?
I thought about…

J. Lennon
- 3,311
- 4
- 33
- 64