// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System.Threading; namespace System.Reactive.Disposables { /// /// Represents a disposable resource that has an associated that will be set to the cancellation requested state upon disposal. /// public sealed class CancellationDisposable : ICancelable { private readonly CancellationTokenSource _cts; /// /// Initializes a new instance of the class that uses an existing . /// /// used for cancellation. /// is null. public CancellationDisposable(CancellationTokenSource cts) { _cts = cts ?? throw new ArgumentNullException(nameof(cts)); } /// /// Initializes a new instance of the class that uses a new . /// public CancellationDisposable() : this(new CancellationTokenSource()) { } /// /// Gets the used by this . /// public CancellationToken Token => _cts.Token; /// /// Cancels the underlying . /// public void Dispose() => _cts.Cancel(); /// /// Gets a value that indicates whether the object is disposed. /// public bool IsDisposed => _cts.IsCancellationRequested; } }