METHOD
CancellationToken.Register
Registers a callback for cancellation and returns a registration to manage its lifetime. When cancellation has already been requested, registration invokes the callback immediately on the registering thread. Keep callbacks short and avoid holding locks they need.
Formal Syntax & Structure
csharp syntaxcsharp
token.Register(callback)Working Example
Example Snippetcsharp
using System;using System.Threading;using System.Threading.Tasks;using var cts = new CancellationTokenSource(); cts.Cancel(); int count = 0; using var r = cts.Token.Register(() => count++); Console.WriteLine(count); // 1