BooleanDisposable.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. namespace System.Reactive.Disposables
  3. {
  4. /// <summary>
  5. /// Represents a disposable resource that can be checked for disposal status.
  6. /// </summary>
  7. public sealed class BooleanDisposable : ICancelable
  8. {
  9. // Keep internal! This is used as sentinel in other IDisposable implementations to detect disposal and
  10. // should never be exposed to user code in order to prevent users from swapping in the sentinel. Have
  11. // a look at the code in e.g. SingleAssignmentDisposable for usage patterns.
  12. internal static readonly BooleanDisposable True = new BooleanDisposable(true);
  13. private volatile bool _isDisposed;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.BooleanDisposable"/> class.
  16. /// </summary>
  17. public BooleanDisposable()
  18. {
  19. }
  20. private BooleanDisposable(bool isDisposed)
  21. {
  22. _isDisposed = isDisposed;
  23. }
  24. /// <summary>
  25. /// Gets a value that indicates whether the object is disposed.
  26. /// </summary>
  27. public bool IsDisposed
  28. {
  29. get { return _isDisposed; }
  30. }
  31. /// <summary>
  32. /// Sets the status to disposed, which can be observer through the <see cref="IsDisposed"/> property.
  33. /// </summary>
  34. public void Dispose()
  35. {
  36. _isDisposed = true;
  37. }
  38. }
  39. }