Create an observable that calls the method again

I would like to re-get the value of the property and assign it to another property, but so far I don't have an Rx Observable handle. How to create and subscribe to an observable that simply reads the property all the time (possibly on a timer or with throttling)?

+4
source share
3 answers

You can use the static Interval operator to periodically highlight a value for a given time interval, and then use the Select operator to convert it to the property value of the object you want to poll.

 var objectIWantToPoll = new MyObject(); var objectIWantToSetPropertyOn = new MyObject(); var polledValues = Observable.Interval(TimeSpan.FromSeconds(1)) .Select(_ => objectIWantToPoll.SomeProperty); polledValues.Subscribe(propertyValue => objectIWantToSetPropertyOn.SomeProperty = propertyValue)); 
+9
source
 public static IObservable<long> CreateObservableTimerWithAction(this Action actionT, int timeSpan, Control control) { var obs = Observable.Create<long>( observer => { Observable.Interval(TimeSpan.FromMilliseconds(timeSpan)) .DistinctUntilChanged(fg =>control.Text ).Subscribe(l => actionT()); return Disposable.Empty; }); return obs; } 

0r:

 public static IObservable<long> CreateObservableTimer<T>(this Action actionT,int timeSpan) { var obs= Observable.Create<long>( observer => { Observable.Interval(TimeSpan.FromMilliseconds(timeSpan)) .DistinctUntilChanged().Subscribe(l => actionT()); return Disposable.Empty; }); return obs; } 

I use this quite often so that temporary methods execute at a specific time until I destroy them (obs.Dispose ()).

CreateObservableTimer (() => CheckForDequeues (1), 500);

I actually sometimes use a long, but most of the time, and not ...

Even use this helper to check schedulers in the priority queue, so you can use it to

+1
source

It looks like you are essentially asking for a survey implementation, where some component polls are for changed values. Observables, as a rule, respond to objects clicked by you (through events / observables / etc), rather than pulling values. Perhaps just setting up the background process on the timer and working with this timer will be enough for your business. Observed. The interval behaves as James Hay mentioned. Remember that Observable.Interval will move your execution context from the dispatcher thread.

Why are you trying to include Rx in your survey implementation? Do you have other observable data sources that you are trying to sync here?

0
source

All Articles