diff options
author | Atsushi Eno <atsushieno@veritas-vos-liberabit.com> | 2012-11-12 22:47:31 +0400 |
---|---|---|
committer | Atsushi Eno <atsushieno@veritas-vos-liberabit.com> | 2012-11-12 22:52:17 +0400 |
commit | d1174f3f8979321a9182925df460e07e08157b41 (patch) | |
tree | d16fb2fc191bf68ff0e2aac600adf71aba8cad01 /Rx.NET/System.Reactive.Core/Reactive/Disposables/CancellationDisposable.cs | |
parent | d90a52595e24b1216c89f6cb5f245262db1810ae (diff) |
partial import of ca05fdeb565e: Reactive Extensions OSS V1.0
Diffstat (limited to 'Rx.NET/System.Reactive.Core/Reactive/Disposables/CancellationDisposable.cs')
-rw-r--r-- | Rx.NET/System.Reactive.Core/Reactive/Disposables/CancellationDisposable.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Rx.NET/System.Reactive.Core/Reactive/Disposables/CancellationDisposable.cs b/Rx.NET/System.Reactive.Core/Reactive/Disposables/CancellationDisposable.cs new file mode 100644 index 0000000..5c67511 --- /dev/null +++ b/Rx.NET/System.Reactive.Core/Reactive/Disposables/CancellationDisposable.cs @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license 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("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
\ No newline at end of file |