从timetamps名单可观测 [英] Observable from list of timetamps
问题描述
如果我有对象的列表,其中包含一个时间戳 - ?我怎样才能让一个可观察的那场大火事件的时间戳之间的相对时间
If I have a list of objects that contains a timestamp - how can I make an observable that fire events at the relative time between the timestamps?
例如。如果我有三个对象时间戳2014年1月1日10:30,2014年1月1日10:45和2014年1月1日11点30分,我想第一个触发事件,向右走,然后在接下来的15分钟后,和最后一个45后分钟
Eg. if I have three objects with timestamps 2014-01-01 10:30, 2014-01-01 10:45 and 2014-01-01 11:30, I want the first event to fire right away, then the next 15 minutes later, and the last one 45 minutes after that.
我怎么能加快这一进程,使1分钟相当于1秒?因此,第一个事件将触发马上像以前一样,但未来会来15秒后,而不是15分钟,等等。
How can I speed up the process, so that 1 minute equals 1 second? So the first event would fire right away as before, but the next would come 15 seconds later instead of 15 minutes and so on.
推荐答案
很多方法可以做到这一点,我敢肯定,但这里用Observable.Generate安排在特定的时间事件的快速采样。它使用的事件的列表 - 但鉴于它与一个迭代器,你可以很容易地适应其使用其它来源:
Lots of ways to do this I'm sure, but here's a quick sample using Observable.Generate to schedule events at a particular time. It uses a list of events - but given it works with an iterator, you can easily adapt it to use another source:
void Main()
{
var now = DateTime.Now;
var events = new List<Tuple<DateTime, string>> {
Tuple.Create(now.AddSeconds(5), "A"),
Tuple.Create(now.AddSeconds(10), "B"),
Tuple.Create(now.AddSeconds(15), "C")
};
var eventSource = Observable.Generate(
(IEnumerator<Tuple<DateTime,string>>)events.GetEnumerator(),
s => s.MoveNext(),
s => s,
s => s.Current.Item2, // the data
s => s.Current.Item1); // the timing
eventSource.Subscribe(Console.WriteLine);
}
此写出A,B和C和5,10和15秒后启动。
This writes out "A", "B" and "C" and 5, 10 and 15 seconds after start-up.
要加速的时候,你可以把围绕着如何安排的时间是除preTED一些逻辑。另一种方法是使用 HistoricalScheduler
。请参阅<一href="http://stackoverflow.com/questions/20969829/advanceable-historical-stream-and-live-stream-in-rx/20975724#20975724">this回答对一些见解。
To speed up time, you can put some logic around how the scheduled time is interpreted. Another way it to use the HistoricalScheduler
. See this answer for some insight on that.
这篇关于从timetamps名单可观测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!