ScheduledDisposable.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Concurrency;
  5. using System.Threading;
  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. /// <summary>
  16. /// 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.
  17. /// </summary>
  18. /// <param name="scheduler">Scheduler where the disposable resource will be disposed on.</param>
  19. /// <param name="disposable">Disposable resource to dispose on the given scheduler.</param>
  20. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="disposable"/> is null.</exception>
  21. public ScheduledDisposable(IScheduler scheduler, IDisposable disposable)
  22. {
  23. if (scheduler == null)
  24. throw new ArgumentNullException(nameof(scheduler));
  25. if (disposable == null)
  26. throw new ArgumentNullException(nameof(disposable));
  27. _scheduler = scheduler;
  28. _disposable = disposable;
  29. }
  30. /// <summary>
  31. /// Gets the scheduler where the disposable resource will be disposed on.
  32. /// </summary>
  33. public IScheduler Scheduler
  34. {
  35. get { return _scheduler; }
  36. }
  37. /// <summary>
  38. /// Gets the underlying disposable. After disposal, the result is undefined.
  39. /// </summary>
  40. public IDisposable Disposable
  41. {
  42. get
  43. {
  44. var current = _disposable;
  45. if (current == BooleanDisposable.True)
  46. return DefaultDisposable.Instance; // Don't leak the sentinel value.
  47. return current;
  48. }
  49. }
  50. /// <summary>
  51. /// Gets a value that indicates whether the object is disposed.
  52. /// </summary>
  53. public bool IsDisposed
  54. {
  55. get { return _disposable == BooleanDisposable.True; }
  56. }
  57. /// <summary>
  58. /// Disposes the wrapped disposable on the provided scheduler.
  59. /// </summary>
  60. public void Dispose()
  61. {
  62. Scheduler.Schedule(DisposeInner);
  63. }
  64. private void DisposeInner()
  65. {
  66. #pragma warning disable 0420
  67. var disposable = Interlocked.Exchange(ref _disposable, BooleanDisposable.True);
  68. #pragma warning restore 0420
  69. if (disposable != BooleanDisposable.True)
  70. {
  71. disposable.Dispose();
  72. }
  73. }
  74. }
  75. }