Observable.Timer():如何避免计时器漂移? [英] Observable.Timer(): How to avoid timer drift?

查看:451
本文介绍了Observable.Timer():如何避免计时器漂移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#(.NET 4.0)应用程序中,我使用响应式扩展(2.0.20823.0)生成时间边界,以将事件分组为聚合值。为了简化对结果数据库的查询,这些边界需要整小时(以下示例中为秒)对齐。

In a C# (.NET 4.0) Application, I use the Reactive Extensions (2.0.20823.0) to generate time boundaries for grouping events into aggregate values. To simplify queries to the resulting database, these boundaries need to be aligned on full hours (or seconds in the example below).

使用 Observable。 Timer()

var time = DefaultScheduler.Instance;

var start = new DateTimeOffset(time.Now.DateTime, time.Now.Offset);

var span = TimeSpan.FromSeconds(1);

start -= TimeSpan.FromTicks(start.Ticks % 10000000);
start += span;

var boundary = Observable.Timer(start, span, time);

boundary.Select(i => start + TimeSpan.FromSeconds(i * span.TotalSeconds))
    .Subscribe(t => Console.WriteLine("ideal: " + t.ToString("HH:mm:ss.fff")));

boundary.Select(i => time.Now)
    .Subscribe(t => Console.WriteLine("actual: " + t.ToString("HH:mm:ss.fff")));

您可以看到计时器刻度的预期时间和实际时间相差很大: p>

You can see that the intended and the actual time of the timer ticks drift apart quite heavily:

ideal: 10:06:40.000
actual: 10:06:40.034
actual: 10:06:41.048
ideal: 10:06:41.000
actual: 10:06:42.055
ideal: 10:06:42.000
ideal: 10:06:43.000
actual: 10:06:43.067
actual: 10:06:44.081
ideal: 10:06:44.000
ideal: 10:06:45.000
actual: 10:06:45.095
actual: 10:06:46.109
ideal: 10:06:46.000
ideal: 10:06:47.000
actual: 10:06:47.123
actual: 10:06:48.137
ideal: 10:06:48.000
...

我还使用了 HistoricalScheduler ,当然我在那里也没有问题。我可以容忍轻微的错误,并且不需要关心系统时钟的变化。这些Observable不会触发任何重量级操作。

I also make use of a HistoricalScheduler and of course I have no problems there. I can tolerate slight inaccuracies and I do not need to care about system clock changes. There are no heavyweight operations triggered by those Observables.

此外,我知道在博客帖子,但我似乎无法解决这个问题。

Also, I know there is a lengthy discussion of RX timer drift problems in this blog post, but I don´t seem to be able to wrap my head around it.

定期安排可观察没有系统的计时器漂移?

What would be the right way to periodically schedule an Observable without systematic timer drift?

推荐答案

您可以使用可观察。生成

var boundary = Observable.Generate(
    0, _ => true, // start condition
    i => ++i,     // iterate
    i => i,       // result selector
    i => start + TimeSpan.FromSeconds(i * span.TotalSeconds),
    time);

这将基于每次迭代的绝对时间重新安排。

This will reschedule based on absolute time every iteration.

以下是一些示例输出:

actual: 01:00:44.003
ideal: 01:00:44.000
actual: 01:00:44.999
ideal: 01:00:45.000
actual: 01:00:46.012
ideal: 01:00:46.000
actual: 01:00:47.011
ideal: 01:00:47.000
actual: 01:00:48.011
ideal: 01:00:48.000
actual: 01:00:49.007
ideal: 01:00:49.000
actual: 01:00:50.009
ideal: 01:00:50.000
actual: 01:00:51.006
ideal: 01:00:51.000

由于汉斯(Hans)解释的原因,它并不完全匹配,

It doesn't match exactly, I imagine due to reasons explained by Hans, but there's no drift.

编辑:

以下是RxSource

// BREAKING CHANGE v2 > v1.x - No more correction for time drift based on absolute time. This
//                             didn't work for large period values anyway; the fractional
//                             error exceeded corrections. Also complicated dealing with system
//                             clock change conditions and caused numerous bugs.
//
// - For more precise scheduling, use a custom scheduler that measures TimeSpan values in a
//   better way, e.g. spinning to make up for the last part of the period. Whether or not the
//   values of the TimeSpan period match NT time or wall clock time is up to the scheduler.
//
// - For more accurate scheduling wrt the system clock, use Generate with DateTimeOffset time
//   selectors. When the system clock changes, intervals will not be the same as diffs between
//   consecutive absolute time values. The precision will be low (1s range by default).

这篇关于Observable.Timer():如何避免计时器漂移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆