ScheduledDisposable.cs 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Threading;
  3. namespace UniRx
  4. {
  5. public sealed class ScheduledDisposable : ICancelable
  6. {
  7. private readonly IScheduler scheduler;
  8. private volatile IDisposable disposable;
  9. private int isDisposed = 0;
  10. public ScheduledDisposable(IScheduler scheduler, IDisposable disposable)
  11. {
  12. this.scheduler = scheduler;
  13. this.disposable = disposable;
  14. }
  15. public IScheduler Scheduler
  16. {
  17. get { return scheduler; }
  18. }
  19. public IDisposable Disposable
  20. {
  21. get { return disposable; }
  22. }
  23. public bool IsDisposed
  24. {
  25. get { return isDisposed != 0; }
  26. }
  27. public void Dispose()
  28. {
  29. Scheduler.Schedule(DisposeInner);
  30. }
  31. private void DisposeInner()
  32. {
  33. if (Interlocked.Increment(ref isDisposed) == 1)
  34. {
  35. disposable.Dispose();
  36. }
  37. }
  38. }
  39. }