Skip to main content

Posts

Showing posts with the label Disposable

Listening to a single value from an Rx observable sequence

I have an observable sequence and I want to observe only a defined number of values, the number is immaterial, the important part is the fact I don't want to be notified of any further values. How can this can be achieved? The answer is easy, use the Rx extension method  Take , it'll return the required number of values only. A simple example: 1: Observable.FromEventPattern<RoutedEventArgs>(clickButton, "Click" ) 2: .Take(1) 3: .ObserveOn(Scheduler.CurrentThread) 4: .Subscribe(vt => Debug.WriteLine( "{0}: Click called..." , ++count)); When the button is clicked in the UI it produces a line in the output window in visual studio: So using the Take method I can limit the number of observations irrespective of the number of times I click the 'Click Me!' button: 1: Observable.FromEventPattern<RoutedEventArgs>(clickButton, "Click" ) 2: .Take(1) 3: .Observ...

Does the Rx subscriber get disposed when OnCompleted is called?

We use an implementation of an observable command - ObservableCommand<T> in our view models not only to wire the actions of controls defined in the XAML but to allow the other interested parties to be notified when the command has executed. We were wondering what happens when the observable command is disposed, does the OnCompleted method for the subscribers get called and importantly are the subscribers disposed? Our observable command has an implementation similar to this: 1:   2: public class ObservableCommand<T> : ICommand, IObservable<T> 3: { 4: private readonly Subject<T> _subj = new Subject<T>(); 5:   6: public void Execute( object parameter) 7: { 8: _subj.OnNext((T)parameter); 9: } 10:   11: public bool CanExecute( object parameter) 12: { 13: return true ; 14: } 15:   16: public even...