C#为什么定时器的频率非常关闭? [英] C# Why are timer frequencies extremely off?

查看:267
本文介绍了C#为什么定时器的频率非常关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者 System.Timers.Timer System.Threading.Timer 防火间隔是相当不同于要求的。
例如:

Both System.Timers.Timer and System.Threading.Timer fire at intervals that are considerable different from the requested ones. For example:

new System.Timers.Timer(1000d / 20);



产生一个触发每秒16次,而不是20计时器。

yields a timer that fires 16 times per second, not 20.

要确保有来自过长的事件处理程序没有副作用,我写了这个小测试程序:

To be sure that there are no side-effects from too long event handlers, I wrote this little test program:

int[] frequencies = { 5, 10, 15, 20, 30, 50, 75, 100, 200, 500 };

// Test System.Timers.Timer
foreach (int frequency in frequencies)
{
    int count = 0;

    // Initialize timer
    System.Timers.Timer timer = new System.Timers.Timer(1000d / frequency);
    timer.Elapsed += delegate { Interlocked.Increment(ref count); };

    // Count for 10 seconds
    DateTime start = DateTime.Now;
    timer.Enabled = true;
    while (DateTime.Now < start + TimeSpan.FromSeconds(10))
        Thread.Sleep(10);
    timer.Enabled = false;

    // Calculate actual frequency
    Console.WriteLine(
        "Requested frequency: {0}\nActual frequency: {1}\n",
        frequency, count / 10d);
}

输出看起来是这样的:

请求:5赫兹;实际:4,8赫兹结果
要求:10赫兹;实际:9,1赫兹结果
要求:15赫兹;实际:12,7赫兹结果
要求:20赫兹;实际:16赫兹结果
要求:30赫兹;实际:21.3赫兹结果
要求:50赫兹;实际:31.8赫兹结果
要求:75赫兹;实际:63,9赫兹结果
要求:100赫兹;实际:63,8赫兹结果
要求:200赫兹;实际:63,9赫兹结果
要求:500赫兹;实际:63,9赫兹

Requested: 5 Hz; actual: 4,8 Hz
Requested: 10 Hz; actual: 9,1 Hz
Requested: 15 Hz; actual: 12,7 Hz
Requested: 20 Hz; actual: 16 Hz
Requested: 30 Hz; actual: 21,3 Hz
Requested: 50 Hz; actual: 31,8 Hz
Requested: 75 Hz; actual: 63,9 Hz
Requested: 100 Hz; actual: 63,8 Hz
Requested: 200 Hz; actual: 63,9 Hz
Requested: 500 Hz; actual: 63,9 Hz

实际频率为从与要求的36%的偏离。 (看样子不能超过64赫兹。)鉴于Microsoft建议此计时器以其更准确过 System.Windows.Forms.Timer ,这令我费解。

The actual frequency deviates by up to 36% from the requested one. (And evidently cannot exceed 64 Hz.) Given that Microsoft recommends this timer for its "greater accuracy" over System.Windows.Forms.Timer, this puzzles me.

顺便说一下,这些都不是随机偏差。它们是相同的值每一次。
和为其他计时器类类似的测试程序, System.Threading.Timer ,显示同样的结果。

Btw, these are not random deviations. They are the same values every time. And a similar test program for the other timer class, System.Threading.Timer, shows the exact same results.

在我实际的程序,我需要在每秒正是50个样品收集测量。这不应该还需要一个实时系统。这是非常令人沮丧得到每秒的,而不是50 32样本。

In my actual program, I need to collect measurements at precisely 50 samples per second. This should not yet require a real-time system. And it is very frustrating to get 32 samples per second instead of 50.

任何想法?

@克里斯:
你说得对,间隔都似乎是周围的东西1/64秒的整数倍。顺便说一句,在事件处理程序添加了Thread.Sleep(...)没有任何区别。这使得因为 System.Threading.Timer 使用线程池,所以每个事件在一个自由线程解雇感。

@Chris: You are right, the intervals all seem to be integer multiples of something around 1/64th second. Btw, adding a Thread.Sleep(...) in the event handler doesn't make any difference. This makes sense given that System.Threading.Timer uses the thread pool, so each event is fired on a free thread.

推荐答案

好吧,我收到不同数量高达100赫兹实际上,一些大的偏差,但在大多数情况下,更接近请求数(与最近的.NET运行XP SP3 SP)中。

Well, I'm getting different number up to 100 Hz actually, with some big deviations, but in most cases closer to the requested number (running XP SP3 with most recent .NET SPs).

System.Timer.Timer使用System.Threading.Timer实现的,所以这就是为什么你看到同样的结果。我想,在定时器使用某种调度算法等。(它的内部调用,也许看着转子2.0可能会脱落一些关于它的光)。

The System.Timer.Timer is implemented using System.Threading.Timer, so this explains why you see same results. I suppose that the timer is implemented using some kind of scheduling algorithm etc. (it's internal call, maybe looking at Rotor 2.0 might shed some light on it).

我会实现建议实现使用另一个线程(或其组合)的一种计时器的调用睡眠和回调。不知道,虽然结果。

I would suggest to implement a kind of timer using another thread (or combination thereof) calling Sleep and a callback. Not sure about the outcome though.

,否则你可能会看一看的多媒体计时器(PInvoke的)。

Otherwise you might take a look at multimedia timers (PInvoke).

这篇关于C#为什么定时器的频率非常关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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