定时器需要10毫秒的间隔相比更 [英] Timer takes 10 ms more than interval

查看:631
本文介绍了定时器需要10毫秒的间隔相比更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是定时器间隔1秒。 但在计时器的滴答事件时,我打印的时候它总是62或65毫秒。 我不明白为什么它需要10毫秒以上。

请能有一个人有考虑这一点。

下面是code我使用的:

 静态INT _counter;
VAR _timer =新System.Timers.Timer的(1000);
公共Form1中()
{
    的InitializeComponent();
    _timer.Elapsed + =新ElapsedEventHandler(_timer_Elapsed);
    _timer.Start();
}

私人无效_timer_Elapsed(对象发件人,ElapsedEventArgs E)
{
    Console.WriteLine(DateTime.Now.ToString({HH:MM:ss.fff格式}));
    _counter ++;
    如果(_counter == 20)
        _timer.Stop();
}
 

这将输出:

  {01:59:08.381}
{01:59:09.393}
{01:59:10.407}
{01:59:11.421}
{01:59:12.435}
{01:59:13.449}
{01:59:14.463}
{01:59:15.477}
{01:59:16.491}
{01:59:17.505}
{01:59:18.519}
{01:59:19.533}
{01:59:20.547}
{01:59:21.561}
{01:59:22.575}
{01:59:23.589}
{01:59:24.603}
{01:59:25.615}
{01:59:26.629}
{01:59:27.643}
 

解决方案

您要明白,Windows不是一个实时操作系统。实时操作系统有计时器的机制,使系统做出何时定时发起的事件发生,很难保证与之相关的开销,并允许您指定在最后期限错过了应该发生什么样的行为 - 例如,如果previous执行花费的时间超过的时间间隔。

我会描述Windows的计时器作为尽力而为,当涉及到更小的区间。当时间足够长,你没有注意到,你没有得到你要求的确切时间。当你越来越接近定时器(定时器运行时的频率)的分辨率,你开始看到的开销,因为间隔增加的百分比。实时系统要特别小心,尽量减少软件开销,依靠更先进和更快的硬件解决方案。在Windows计时器的确切频率取决于底层硬件提供,因此可能不同于系统到系统的定时服务。

如果您有实时的需求 - 以及做一些每隔50ms可能属于这一类 - 那么你可能需要看看专门的硬件和/或实时操作系统

I am using a timer with interval 1 second. But in the timer's tick event when I print the time it's always 62 or 65 ms. I don't understand why it's taking 10 ms more.

Please can some one have look into this.

Here is the code I am using:

static int _counter;
var _timer = new System.Timers.Timer(1000);
public Form1()
{
    InitializeComponent();           
    _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
    _timer.Start();            
}

private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
    Console.WriteLine(DateTime.Now.ToString("{hh:mm:ss.fff}"));          
    _counter++;
    if (_counter == 20)
        _timer.Stop();
}

And this the output:

{01:59:08.381}
{01:59:09.393}
{01:59:10.407}
{01:59:11.421}
{01:59:12.435}
{01:59:13.449}
{01:59:14.463}
{01:59:15.477}
{01:59:16.491}
{01:59:17.505}
{01:59:18.519}
{01:59:19.533}
{01:59:20.547}
{01:59:21.561}
{01:59:22.575}
{01:59:23.589}
{01:59:24.603}
{01:59:25.615}
{01:59:26.629}
{01:59:27.643}

解决方案

You need to understand that Windows is not a real-time operating system. Real-time operating systems have timer mechanisms that allow the system to make hard guarantees about when timer-initiated events occur and the overhead associated with them, and allow you to specify what behavior should occur when the deadline is missed -- for example if the previous execution took longer than the interval.

I would characterize the Windows timers as "best effort" when it comes to smaller intervals. When the interval is sufficiently long you don't notice that you aren't getting the exact interval that you requested. As you get closer and closer to the resolution of the timer (the frequency at which the timer runs), you start seeing the overhead as a percentage of the interval increase. Real-time systems take special care to minimize the software overhead, relying on more sophisticated and faster hardware solutions. The exact frequency of the Windows timer depends on the timing services that the underlying hardware provides and so may differ from system to system.

If you have real-time needs -- and doing something every 50ms may fall into that category -- then you may need to look at specialized hardware and/or a real-time OS.

这篇关于定时器需要10毫秒的间隔相比更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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