I would like to repeatedly get a properties value and assign it to another property, but I don't have a handle on Rx's Observable
creation quite yet. How do I create and subscribe to an observable that just constantly reads a property (maybe on a timer or with throttling)?
You can use the static Interval
operator to emit a value repeatedly on a given time span and then use a Select
opertor to transform it to the property value on the object you wish 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));
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 , to have timed methods go at certain time , until i dispose of them (obs.Dispose() )..
CreateObservableTimer(() => CheckForDequeues(1),500);
I actually sometimes use the long, but most of the times , not...
Even use this helper to check Schedulers in a priority queue, so could be used to
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With