开发者

Using Reactive Extensions to create a single event?

开发者 https://www.devze.com 2023-03-14 10:40 出处:网络
I\'m trying to get my head around .NET Reactive Extensions, and I wonder if they can be used in the follow scenario:

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消