123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- #if !NO_TPL
- using System.Threading;
- namespace System.Reactive.Disposables
- {
- /// <summary>
- /// Represents a disposable resource that has an associated <seealso cref="T:System.Threading.CancellationToken"/> that will be set to the cancellation requested state upon disposal.
- /// </summary>
- public sealed class CancellationDisposable : ICancelable
- {
- private readonly CancellationTokenSource _cts;
- /// <summary>
- /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CancellationDisposable"/> class that uses an existing <seealso cref="T:System.Threading.CancellationTokenSource"/>.
- /// </summary>
- /// <param name="cts"><seealso cref="T:System.Threading.CancellationTokenSource"/> used for cancellation.</param>
- /// <exception cref="ArgumentNullException"><paramref name="cts"/> is null.</exception>
- public CancellationDisposable(CancellationTokenSource cts)
- {
- if (cts == null)
- throw new ArgumentNullException(nameof(cts));
- _cts = cts;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CancellationDisposable"/> class that uses a new <seealso cref="T:System.Threading.CancellationTokenSource"/>.
- /// </summary>
- public CancellationDisposable()
- : this(new CancellationTokenSource())
- {
- }
- /// <summary>
- /// Gets the <see cref="T:System.Threading.CancellationToken"/> used by this CancellationDisposable.
- /// </summary>
- public CancellationToken Token
- {
- get { return _cts.Token; }
- }
- /// <summary>
- /// Cancels the underlying <seealso cref="T:System.Threading.CancellationTokenSource"/>.
- /// </summary>
- public void Dispose()
- {
- _cts.Cancel();
- }
- /// <summary>
- /// Gets a value that indicates whether the object is disposed.
- /// </summary>
- public bool IsDisposed
- {
- get { return _cts.IsCancellationRequested; }
- }
- }
- }
- #endif
|