<PackageReference Include="System.Reactive" Version="6.0.0-preview.1" />

CancellationDisposable

Represents a disposable resource that has an associated CancellationToken that will be set to the cancellation requested state upon disposal.
using System.Runtime.CompilerServices; using System.Threading; namespace System.Reactive.Disposables { public sealed class CancellationDisposable : ICancelable, IDisposable { [System.Runtime.CompilerServices.Nullable(1)] private readonly CancellationTokenSource _cts; public CancellationToken Token => _cts.Token; public bool IsDisposed => _cts.IsCancellationRequested; [System.Runtime.CompilerServices.NullableContext(1)] public CancellationDisposable(CancellationTokenSource cts) { if (cts == null) throw new ArgumentNullException("cts"); _cts = cts; } public CancellationDisposable() : this(new CancellationTokenSource()) { } public void Dispose() { _cts.Cancel(); } } }