Note: All samples are verified to work with Rx build 1.0.2350 unless otherwise noted.
Mutable
Summary
A MutableDisposable is a disposable that can have its implementation changed at any time. The act of changing the disposable implementation causes the current disposable to dispose.
Example
private static void Mutable() { IDisposable disposable = SimulatedWebService(false) .Subscribe(x => Debug.WriteLine("Returned: " + x.ToString())); } private static void Mutable2() { IDisposable disposable = SimulatedWebService(false) .Subscribe(x => Debug.WriteLine("Returned: " + x.ToString())); disposable.Dispose(); Debug.WriteLine("Canceled"); } private static IObservable<int> SimulatedWebService(bool sendError) { return Observable.CreateWithDisposable<int>(observer => { MutableDisposable mutable = new MutableDisposable(); bool cancel = false; Scheduler.NewThread.Schedule(() => { if (!cancel) { if (sendError) { mutable.Replace(Observable .Throw<int>(new Exception("Sample error message.")) .Subscribe(observer)); } else { mutable.Replace(Observable .Return(new Random().Next()) .Subscribe(observer)); } } }, DateTimeOffset.Now.AddSeconds(2)); mutable.Replace(Disposable.Create(() => cancel = true)); return mutable; }); }
Future
Summary
A FutureDisposable is a disposable that is not required to have implementation before it is disposed. If the FutureDisposable is disposed before it has received an implementation, then when an implementation is provided it will be immediately disposed.
Example
// This will allow numbers below 5 to come through, // and then dispose. private static void Future() { FutureDisposable future = new FutureDisposable(); var observable = Observable.Range(1, 10, Scheduler.NewThread) .Do(x => Debug.WriteLine(x)) .Where(x => x > 5) .Do(x => future.Dispose()); future.Set(observable.Subscribe()); }
Composite
Summary
A CompsiteDisposable contains a set of disposables that will be disposed when the CompositeDisposable is disposed.
Example
private static void Composite() { CompositeDisposable composite = new CompositeDisposable(); composite.Add(Observable .Interval(TimeSpan.FromSeconds(0.5)) .Select(x => "Interval 0.5: " + x.ToString()) .Do(message => Debug.WriteLine(message)) .Subscribe()); composite.Add(Observable .Interval(TimeSpan.FromSeconds(0.3)) .Select(x => "Interval 0.3: " + x.ToString()) .Do(message => Debug.WriteLine(message)) .Subscribe()); composite.Add(Observable .Interval(TimeSpan.FromSeconds(0.1)) .Select(x => "Interval 0.1: " + x.ToString()) .Do(message => Debug.WriteLine(message)) .Subscribe()); Scheduler.NewThread.Schedule( composite.Dispose, DateTimeOffset.Now.AddSeconds(2)); }
Context
Summary
A ContextDisposable will execute its disposable implementation on the specified SynchronizationContext.
Example
private static void Context() { ContextDisposable context = new ContextDisposable( SynchronizationContext.Current, Disposable.Create(() => Debug.WriteLine("Disposing Thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString()))); Scheduler.ThreadPool.Schedule(() => { Debug.WriteLine("Calling Thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString()); context.Dispose(); }); }
Boolean
Summary
A BooleanDisposable has a Boolean flag that will be set to true when it has been disposed.
Example
private static void Bool() { BooleanDisposable boolDisposable = new BooleanDisposable(); if (!boolDisposable.IsDisposed) { Debug.WriteLine("Disposing"); boolDisposable.Dispose(); } if (boolDisposable.IsDisposed) { Debug.WriteLine("Succesfully Disposed"); } }
Cancel
Summary
A CancelDisposable has a CancellationToken that will be cancelled when the disposable has been disposed.
Example
private static void Cancel() { CancellationDisposable cancel = new CancellationDisposable(); cancel.Token.Register(() => Debug.WriteLine("Disposed from cancellation token")); Debug.WriteLine("Disposing"); cancel.Dispose(); }
RefCount
Summary
A RefCountDisposable prevents the underlying disposable from being disposed until all references have been disposed. References appear in the form of a disposable that decrements the reference counter.
Example
private static void RefCount() { RefCountDisposable refDisposable = new RefCountDisposable( Disposable.Create(() => Debug.WriteLine("Underlying disposable has been disposed"))); IDisposable ref1 = refDisposable.GetDisposable(); IDisposable ref2 = refDisposable.GetDisposable(); IDisposable ref3 = refDisposable.GetDisposable(); Debug.WriteLine("Disposing ref2"); ref2.Dispose(); Debug.WriteLine("Disposing RefCountDisposable"); refDisposable.Dispose(); Debug.WriteLine("Disposing ref3"); ref3.Dispose(); Debug.WriteLine("Disposing ref1"); ref1.Dispose(); }