The CancellationToken is a .NET struct that enables cooperative cancellation of synchronous or asynchronous methods.
Questions tagged [cancellation-token]
366 questions
34
votes
7 answers
Get Task CancellationToken
Can I get CancellationToken which was passed to Task constructor during task action executing. Most of samples look like this:
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
Task myTask =…

SiberianGuy
- 24,674
- 56
- 152
- 266
28
votes
2 answers
Why CancellationTokenRegistration exists and why does it implement IDisposable
I've been seeing code that uses Cancellation.Register with a using clause on the CancellationTokenRegistration result:
using (CancellationTokenRegistration ctr = token.Register(() => wc.CancelAsync()))
{
await wc.DownloadStringAsync(new…

i3arnon
- 113,022
- 33
- 324
- 344
27
votes
6 answers
TaskCanceledException when calling Task.Delay with a CancellationToken in an keyboard event
I am trying to delay the processing of a method (SubmitQuery() in the example) called from an keyboard event in WinRT until there has been no further events for a time period (500ms in this case).
I only want SubmitQuery() to run when I think the…

Andrew Roberts
- 990
- 2
- 12
- 26
26
votes
3 answers
Terminate or exit C# Async method with "return"
I was new to the async-await method in C# 5.0, and I have few questions in my mind
What is the best way to escape an async method if it failed an input argument or null check?
What is the logical flow of using return; in an Task async method (In…

alextcl
- 263
- 1
- 3
- 5
25
votes
5 answers
Can I cancel StreamReader.ReadLineAsync with a CancellationToken?
When I cancel my async method with the following content by calling the Cancel() method of my CancellationTokenSource, it will stop eventually. However since the line Console.WriteLine(await reader.ReadLineAsync()); takes quite a bit to complete, I…

H W
- 2,556
- 3
- 21
- 45
24
votes
1 answer
Is catching TaskCanceledException and checking Task.Canceled a good idea?
There are some people on my team who really love coding with async Task. And sometimes they like to use CancellationToken parameters.
What I'm unsure about is whether we should as a team be using this style of code (A):
async Task…

Tim Lovell-Smith
- 15,310
- 14
- 76
- 93
24
votes
1 answer
What is the use of passing CancellationToken to Task Class constructor?
Here is a sample code that creates a new task that simulates a long running process. There is nothing much on the task as such and purely focuses on the cancelling features. I am using cancellation token to cancel the task and the code works fine…

Prabhu Murthy
- 9,031
- 5
- 29
- 36
22
votes
2 answers
GetResponseAsync does not accept cancellationToken
It seems that GetResponseAsync does not accept cancellationToken in Async/Await. So the question is how can I cancel the below procedure, provided I need to collect Cookies from response:
using (HttpWebResponse response = (HttpWebResponse) await…

Jim
- 2,760
- 8
- 42
- 66
21
votes
1 answer
Faulted vs Canceled task status after CancellationToken.ThrowIfCancellationRequested
Usually I don't post a question with the answer, but this time I'd like to attract some attention to what I think might be an obscure yet common issue. It was triggered by this question, since then I reviewed my own old code and found some of it was…

noseratio
- 59,932
- 34
- 208
- 486
20
votes
1 answer
Cancellation token in Lambda Function Handler C#
Does AWS Lambda Function Handlers in C# provide a cancellation token?
I've read the documentation on AWS site (https://docs.aws.amazon.com/lambda/latest/dg/dotnet-programming-model-handler-types.html) but I can't see anywhere that mentions…

Kevin Smith
- 13,746
- 4
- 52
- 77
17
votes
3 answers
Azure Functions using Cancellation Token with Http Trigger
I am developing a Function in Azure with Cancellation Token. Its an Http Trigger.
I pass in a Cancellation Token in in the method parameters.
Its long running function. And I cancel the request in between of the process but the process keeps running…

Noob Coder
- 444
- 1
- 5
- 16
17
votes
2 answers
What is "cancellationToken" in the TaskFactory.StartNew() used for?
http://msdn.microsoft.com/en-us/library/dd988458.aspx
UPD:
so, let's discuss this article then: http://msdn.microsoft.com/en-us/library/dd997396.aspx
I've changed that code a little:
static void Main()
{
var tokenSource2 = new…

zerkms
- 249,484
- 69
- 436
- 539
16
votes
1 answer
CancellationToken Cancel not breaking out of BlockingCollection
I have a cancellation token like so
static CancellationTokenSource TokenSource= new CancellationTokenSource();
I have a blocking collection like so
BlockingCollection

Kenoyer130
- 6,874
- 9
- 51
- 73
13
votes
3 answers
How to cancel manually a BackgroundService in ASP.net core
I create a BackgroundService like this:
public class CustomService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
do
{
//...
await…

Mehdi
- 903
- 3
- 10
- 26
13
votes
4 answers
Cancellation Token Injection
I'd like to be able to pass cancellation tokens via dependency injection instead of as parameters every time. Is this a thing?
We have an asp.net-core 2.1 app, where we pass calls from controllers into a maze of async libraries, handlers, and other…

user326608
- 2,210
- 1
- 26
- 33