| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Reactive.Concurrency; |
| 4 |
using System.Threading; |
| 5 |
|
| 6 |
namespace System.Reactive.Disposables |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// Represents a disposable resource whose disposal invocation will be scheduled on the specified <seealso cref="T:System.Reactive.Concurrency.IScheduler"/>. |
| 10 |
/// </summary> |
| 11 |
public sealed class ScheduledDisposable : ICancelable |
| 12 |
{ |
| 13 |
private readonly IScheduler _scheduler; |
| 14 |
private volatile IDisposable _disposable; |
| 15 |
|
| 16 |
/// <summary> |
| 17 |
/// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.ScheduledDisposable"/> class that uses an <see cref="T:System.Reactive.Concurrency.IScheduler"/> on which to dispose the disposable. |
| 18 |
/// </summary> |
| 19 |
/// <param name="scheduler">Scheduler where the disposable resource will be disposed on.</param> |
| 20 |
/// <param name="disposable">Disposable resource to dispose on the given scheduler.</param> |
| 21 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="disposable"/> is null.</exception> |
| 22 |
public ScheduledDisposable(IScheduler scheduler, IDisposable disposable) |
| 23 |
{ |
| 24 |
if (scheduler == null) |
| 25 |
throw new ArgumentNullException("scheduler"); |
| 26 |
if (disposable == null) |
| 27 |
throw new ArgumentNullException("disposable"); |
| 28 |
|
| 29 |
_scheduler = scheduler; |
| 30 |
_disposable = disposable; |
| 31 |
} |
| 32 |
|
| 33 |
/// <summary> |
| 34 |
/// Gets the scheduler where the disposable resource will be disposed on. |
| 35 |
/// </summary> |
| 36 |
public IScheduler Scheduler |
| 37 |
{ |
| 38 |
get { return _scheduler; } |
| 39 |
} |
| 40 |
|
| 41 |
/// <summary> |
| 42 |
/// Gets the underlying disposable. After disposal, the result is undefined. |
| 43 |
/// </summary> |
| 44 |
public IDisposable Disposable |
| 45 |
{ |
| 46 |
get |
| 47 |
{ |
| 48 |
var current = _disposable; |
| 49 |
|
| 50 |
if (current == BooleanDisposable.True) |
| 51 |
return DefaultDisposable.Instance; // Don't leak the sentinel value. |
| 52 |
|
| 53 |
return current; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/// <summary> |
| 58 |
/// Gets a value that indicates whether the object is disposed. |
| 59 |
/// </summary> |
| 60 |
public bool IsDisposed |
| 61 |
{ |
| 62 |
get { return _disposable == BooleanDisposable.True; } |
| 63 |
} |
| 64 |
|
| 65 |
/// <summary> |
| 66 |
/// Disposes the wrapped disposable on the provided scheduler. |
| 67 |
/// </summary> |
| 68 |
public void Dispose() |
| 69 |
{ |
| 70 |
Scheduler.Schedule(DisposeInner); |
| 71 |
} |
| 72 |
|
| 73 |
private void DisposeInner() |
| 74 |
{ |
| 75 |
#pragma warning disable 0420 |
| 76 |
var disposable = Interlocked.Exchange(ref _disposable, BooleanDisposable.True); |
| 77 |
#pragma warning restore 0420 |
| 78 |
|
| 79 |
if (disposable != BooleanDisposable.True) |
| 80 |
{ |
| 81 |
disposable.Dispose(); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |