| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_TPL |
| 4 |
using System.Threading; |
| 5 |
|
| 6 |
namespace System.Reactive.Disposables |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// 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. |
| 10 |
/// </summary> |
| 11 |
public sealed class CancellationDisposable : ICancelable |
| 12 |
{ |
| 13 |
private readonly CancellationTokenSource _cts; |
| 14 |
|
| 15 |
/// <summary> |
| 16 |
/// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CancellationDisposable"/> class that uses an existing <seealso cref="T:System.Threading.CancellationTokenSource"/>. |
| 17 |
/// </summary> |
| 18 |
/// <param name="cts"><seealso cref="T:System.Threading.CancellationTokenSource"/> used for cancellation.</param> |
| 19 |
/// <exception cref="ArgumentNullException"><paramref name="cts"/> is null.</exception> |
| 20 |
public CancellationDisposable(CancellationTokenSource cts) |
| 21 |
{ |
| 22 |
if (cts == null) |
| 23 |
throw new ArgumentNullException("cts"); |
| 24 |
|
| 25 |
_cts = cts; |
| 26 |
} |
| 27 |
|
| 28 |
/// <summary> |
| 29 |
/// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CancellationDisposable"/> class that uses a new <seealso cref="T:System.Threading.CancellationTokenSource"/>. |
| 30 |
/// </summary> |
| 31 |
public CancellationDisposable() |
| 32 |
: this(new CancellationTokenSource()) |
| 33 |
{ |
| 34 |
} |
| 35 |
|
| 36 |
/// <summary> |
| 37 |
/// Gets the <see cref="T:System.Threading.CancellationToken"/> used by this CancellationDisposable. |
| 38 |
/// </summary> |
| 39 |
public CancellationToken Token |
| 40 |
{ |
| 41 |
get { return _cts.Token; } |
| 42 |
} |
| 43 |
|
| 44 |
/// <summary> |
| 45 |
/// Cancels the underlying <seealso cref="T:System.Threading.CancellationTokenSource"/>. |
| 46 |
/// </summary> |
| 47 |
public void Dispose() |
| 48 |
{ |
| 49 |
_cts.Cancel(); |
| 50 |
} |
| 51 |
|
| 52 |
/// <summary> |
| 53 |
/// Gets a value that indicates whether the object is disposed. |
| 54 |
/// </summary> |
| 55 |
public bool IsDisposed |
| 56 |
{ |
| 57 |
get { return _cts.IsCancellationRequested; } |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
#endif |