I'm trying to get my head around .NET Reactive Extensions, and I wonder if they can be used in the follow scenario:
In my WP7 app, I'm using the SterlingDatabase to persist app settings. As a user is modifying the settings, I want 开发者_如何转开发to periodically call Database.Flush()
So in my Property Set method, I'd like to kick off a Database.Flush() timer event, and in say 5 seconds, write to the database. If another property is written to, I want to restart the timer.
I know I can do this with a timer object, calling Start()
and Stop()
, but I wanted to know if I could do this with Rx, to create an Asycn operation that I can basically start and stop, without using a timer?
Use Throttle:
public void AttachFlushes(IObservable<Unit> writes, SterlingDb db)
{
writes.Throttle(TimeSpan.FromSeconds(5)).Do(_ => db.Flush()).Subscribe();
}
精彩评论