<PackageReference Include="System.Reactive" Version="4.2.2" />

ScheduledDisposable

Represents a disposable resource whose disposal invocation will be scheduled on the specified IScheduler.
using System.Reactive.Concurrency; namespace System.Reactive.Disposables { public sealed class ScheduledDisposable : ICancelable, IDisposable { private IDisposable _disposable; public IScheduler Scheduler { get; } public IDisposable Disposable => System.Reactive.Disposables.Disposable.GetValueOrDefault(ref _disposable); public bool IsDisposed => System.Reactive.Disposables.Disposable.GetIsDisposed(ref _disposable); public ScheduledDisposable(IScheduler scheduler, IDisposable disposable) { if (disposable == null) throw new ArgumentNullException("disposable"); if (scheduler == null) throw new ArgumentNullException("scheduler"); Scheduler = scheduler; System.Reactive.Disposables.Disposable.SetSingle(ref _disposable, disposable); } public void Dispose() { Scheduler.ScheduleAction(this, delegate(ScheduledDisposable scheduler) { System.Reactive.Disposables.Disposable.TryDispose(ref scheduler._disposable); }); } } }