METHOD

CancellationTokenRegistration.Dispose

Removes a cancellation callback registration. Disposal may wait for a running callback, except when the callback disposes its own registration. Do not hold a lock required by the callback during disposal.

Formal Syntax & Structure

csharp syntaxcsharp
registration.Dispose()

Working Example

Example Snippetcsharp
using System;using System.Threading;using System.Threading.Tasks;using var cts = new CancellationTokenSource(); int count = 0; var r = cts.Token.Register(() => count++); r.Dispose(); cts.Cancel(); Console.WriteLine(count); // 0
C# & .NET Reference