开发者

How can I start an IObservable on a specific time?

Assuming I use Rx, How can I create an IObservable which will fire an event every 5 seconds how开发者_如何转开发 can I make the observable start on a specific date ? How can I make it fire its events on a round date (every midnight ?)


How can I create an IObservable which will fire an event every 5 seconds

Observable.Interval(TimeSpan.FromSeconds(5))

how can I make the observable start on a specific date

You can use Timer for this:

// Fire at Jan 2014.
var jan2014 = new DateTimeOffset(2014, 1, 1, 0, 0, 0);
Observable.Timer(jan2014);

How can I make it fire its events on a round date (every midnight ?)

Timer has an overload that lets you fire it within a certain due time, then every set time span. It should look something like this:

// Fire at Jan 2014 at midnight, then every 24 hours after that (e.g. every midnight):
var jan2014 = new DateTimeOffset(2014, 1, 1, 0, 0, 0);
Observable.Timer(jan2014, TimeSpan.FromHours(24));


// Do you really care if it's up to five seconds later than midnight?
Observable.Interval(TimeSpan.FromSeconds(5))
          .Where(x => DateTime.Now > new DateTime(1,2,2010));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜