Should I always add CancellationToken to my controller actions?

Should I always add CancellationToken to my controller actions? No. You should not always. Using CancellationTokens in ASP.NET Core MVC controllers https://andrewlock.net/using-cancellationtokens-in-asp-net-core-mvc-controllers/ Whether this is correct behaviour will depend on your app. If the request modifies state, then you may not want to halt execution mid-way through a method. On the other hand, if the … Read more

Linking Cancellation Tokens

You want to use CancellationTokenSource.CreateLinkedTokenSource. It allows to have a “parent” and a “child” CancellationTokenSourcees. Here’s a simple example: var parentCts = new CancellationTokenSource(); var childCts = CancellationTokenSource.CreateLinkedTokenSource(parentCts.Token); childCts.CancelAfter(1000); Console.WriteLine(“Cancel child CTS”); Thread.Sleep(2000); Console.WriteLine(“Child CTS: {0}”, childCts.IsCancellationRequested); Console.WriteLine(“Parent CTS: {0}”, parentCts.IsCancellationRequested); Console.WriteLine(); parentCts.Cancel(); Console.WriteLine(“Cancel parent CTS”); Console.WriteLine(“Child CTS: {0}”, childCts.IsCancellationRequested); Console.WriteLine(“Parent CTS: {0}”, parentCts.IsCancellationRequested); Output … Read more

How to reset a CancellationToken properly?

You need to recreate the CancellationTokenSource – there is no way to “reset” this once you set it. This could be as simple as: private void Button_Click(object sender, RoutedEventArgs e) { if (button.Content == “Start”) { button.Content = “Stop”; cts.Dispose(); // Clean up old token source…. cts = new CancellationTokenSource(); // “Reset” the cancellation token … Read more

Default parameter for CancellationToken

It turns out that the following works: Task<x> DoStuff(…., CancellationToken ct = default(CancellationToken)) …or: Task<x> DoStuff(…., CancellationToken ct = default) // C# 7.1 and later which, according to the documentation, is interpreted the same as CancellationToken.None: You can also use the C# default(CancellationToken) statement to create an empty cancellation token.

How to use the CancellationToken property?

You can implement your work method as follows: private static void Work(CancellationToken cancelToken) { while (true) { if(cancelToken.IsCancellationRequested) { return; } Console.Write(“345”); } } That’s it. You always need to handle cancellation by yourself – exit from method when it is appropriate time to exit (so that your work and data is in consistent state) … Read more

Why CancellationToken is separate from CancellationTokenSource?

I was involved in the design and implementation of these classes. The short answer is “separation of concerns“. It is quite true that there are various implementation strategies and that some are simpler at least regarding the type system and initial learning. However, CTS and CT are intended for use in a great many scenarios … Read more

When to dispose CancellationTokenSource?

Speaking about whether it’s really necessary to call Dispose on CancellationTokenSource… I had a memory leak in my project and it turned out that CancellationTokenSource was the problem. My project has a service, that is constantly reading database and fires off different tasks, and I was passing linked cancellation tokens to my workers, so even … Read more

Cancellation token in Task constructor: why?

Passing a CancellationToken into the Task constructor associates it with the task. Quoting Stephen Toub’s answer from MSDN: This has two primary benefits: If the token has cancellation requested prior to the Task starting to execute, the Task won’t execute. Rather than transitioning to Running, it’ll immediately transition to Canceled. This avoids the costs of … Read more