<PackageReference Include="System.Reactive" Version="4.1.4" />

CancellationDisposable

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