The CancellationToken is a .NET struct that enables cooperative cancellation of synchronous or asynchronous methods.
Questions tagged [cancellation-token]
366 questions
13
votes
2 answers
Why CancellationToken is a struct?
Does it make any sense to use a struct instead of a reference type in case of CancellationToken?
I see one possible disadvantage, it will be copied all the way down in methods chain as I pass it as a parameter.
In the same time, as far as it is…

Neir0
- 12,849
- 28
- 83
- 139
12
votes
3 answers
c# lock and listen to CancellationToken
I want to use lock or a similar synchronization to protect a critical section. At the same time I want to listen to a CancellationToken.
Right now I'm using a mutex like this, but mutex doesn't have as good performance. Can I use any of other…

Karsten
- 8,015
- 8
- 48
- 83
12
votes
4 answers
Wait for request of CancellationToken cancellation
How can I pause the executing until the cancellation is requested?
var cts = new CancellationTokenSource();
Task.Run(() =>
{
// Wait for the Cancel...
…

user4388177
- 2,433
- 3
- 16
- 30
12
votes
1 answer
How can a default(CancellationToken) have a corresponding CancellationTokenSource
When I create a default CancellationToken I can see in the debugger that the CancellationToken has a CancellationTokenSource associated with it which is stored in the private m_source field:
I am wondering how can that be as for structs the default…

i3arnon
- 113,022
- 33
- 324
- 344
11
votes
1 answer
Why is the task is not cancelled when I call CancellationTokenSource's Cancel method in async method?
I created a small wrapper around CancellationToken and CancellationTokenSource. The problem I have is that the CancelAsync method of CancellationHelper doesn't work as expected.
I'm experiencing the problem with the…

Balázs Szántó
- 1,440
- 3
- 15
- 29
11
votes
1 answer
When does f# async check its CancellationToken?
I'm reading F# for fun and profit - Asynchronous programming. Under Cancelling workflows they have the following example:
let testLoop = async {
for i in [1..100] do
// do something
printf "%i before.." i
// sleep a bit
do!…

johv
- 4,424
- 3
- 26
- 42
10
votes
2 answers
HttpClient cancellation doesn't kill underlying TCP call
I'm trying to set a default timeout for my HttpClient calls to 5 seconds.
I've done this via CancellationTokenSource.
Here's the pertinent bit of code:
var cancellationToken = new…

RPM1984
- 72,246
- 58
- 225
- 350
10
votes
2 answers
How to create a cancelled task
I am writing a Stream class and am blocked in the ReadAsync method. Please take a look at the code, I think it can explain the situation better that I can do it with my English.
public override Task ReadAsync(byte[] buffer, int offset, int…

lontivero
- 5,235
- 5
- 25
- 42
9
votes
2 answers
Why is IsCancellationRequested not set to true on stopping a BackgroundService in .NET Core 3.1?
I've read most articles I can find about IHostApplicationLifetime and CancellationToken's in .NET Core 3.1, but I cannot find a reason why this is not working.
I have a simple BackgroundService which look like the following:
public class…

Jim Aho
- 9,932
- 15
- 56
- 87
9
votes
2 answers
Stop Exception generated by CancellationToken from getting Reported by ApplicationInsights
I thought it would be a good idea to use CancellationToken in my controller like so:
[HttpGet("things", Name = "GetSomething")]
public async Task GetSomethingAsync(CancellationToken ct) {
var result = await…

Daniel Gimenez
- 18,530
- 3
- 50
- 70
9
votes
1 answer
MongoDB C# driver CancellationToken
Does anyone know what the CancellationToken does if you add it with a parameter in the for example
public static UpdateResult UpdateMany(
this IMongoCollection collection,
Expression> filter,
…

StuiterSlurf
- 2,464
- 4
- 34
- 55
9
votes
0 answers
Why doesn't a local variable of type CancellationToken need initialization?
I'm using VisualStudio 2017 on a project that targets .NET Framework 4.6.1.
Playing with Task, CancellationToken and a local method, I came to this code:
class Program
{
static void Main(string[] args)
{
CancellationToken o;
…

Orace
- 7,822
- 30
- 45
9
votes
1 answer
Correct use of CancellationToken
This is my situation:
private CancellationTokenSource cancellationTokenSource;
private CancellationToken cancellationToken;
public IoTHub()
{
cancellationTokenSource = new CancellationTokenSource();
cancellationToken…

BlueCastle
- 217
- 2
- 8
- 18
9
votes
2 answers
TaskCancellationException how to avoid the exception on success control flow?
In our application we work a lot with async / await and Tasks. Therefore it does use Task.Run a lot, sometimes with cancellation support using the built in CancellationToken.
public Task DoSomethingAsync(CancellationToken cancellationToken)
{
…

quadroid
- 8,444
- 6
- 49
- 82
9
votes
1 answer
Techniques for exiting / cancelling while loops across threads: bool, ManualResetEvent or CancellationToken
I am writing a program that has a few threads, each with a while loop that runs until the user specifies it should stop. I thought of a few ways to exit out of the loops, and subsequently the threads, and have outlined these approaches…

Anders
- 580
- 8
- 17