计时器触发滴答声事件,延迟时间为15毫秒 [英] Timer firing Tick event with 15 milliseconds delay

查看:493
本文介绍了计时器触发滴答声事件,延迟时间为15毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在计时器方面遇到了奇怪的问题.据我所知,计时器的interval属性指示将在其中触发timer_Tick事件的循环之间的延迟.

I'm having a weird problem with timers. As long as I know, the interval property of a timer indicates the delay between loops in which the timer_Tick event will be fired.

在Visual Basic中进行编程之前,我遇到了确切的签名(延迟15和16毫秒)的问题.我创建的任何计时器都会以15或16毫秒的延迟触发其tick事件.例如,如果我将计时器的时间间隔设置为1(这意味着它的滴答事件应在1秒内触发1000次),则该事件将在1秒内被触发62至66次(即1000/16至1000/15) ).

I had this problem with the exact signature (15 and 16 milliseconds delay) before while programming in Visual Basic. Any timers that I create, fire their tick event with 15 or 16 milliseconds delay. For instance, if I set the interval of my timer to 1 (which means its tick event should get fired 1000 times in 1 second), the event instead gets fired 62 to 66 times in 1 second (that's 1000/16 to 1000/15).

自5年前以来,我一直在开发VB应用程序,并且始终会遇到这个问题(这也意味着我在使用AMD和Intel处理器的多个不同系统上都遇到了这个问题),现在我又在C#中遇到了这个问题.

I've been developing VB applications since 5 years ago and always had this problem (that also means I had this problem on several different systems with both AMD and Intel processors), and now I'm having it again with C#.

我设法通过基于TickCount方法(VB中的GetTickCount API和C#中的Environment.TickCount)计算滴答事件每次触发之间的时间差来设法解决此问题,并解决了这个问题.

I'd managed to do a workaround and solve this problem by calculating the time difference between each time the tick event gets fired based on TickCount method (GetTickCount API in VB and Environment.TickCount in C#).

* TickCount是系统开始计时以来经过的毫秒数.

*TickCount is the amount of milliseconds passed since the time system had started.

为了更好地理解该问题,我创建了一个Windows应用程序,该应用程序计算自执行以来的秒数(如计时器).每次触发Tick事件时,它都依赖于TickCount和普通加法.它还通过从TickCount的当前值中减去TickCount的最后一个值来计算计时器的延迟(如果计时器实际上在一秒钟内会被触发1000次,则TickCounts的差每次将为1,因此表示没有延迟,但是如果差异大于1,则每次触发计时器的滴答事件之间都会有一些延迟.

To understand the problem better, I created a windows application that counts the seconds since it gets executed (like a timer). It relies on both TickCount and ordinary addition on each time the Tick event gets fired. It also calculates the delay of the timer by subtracting the last value of TickCount from the current value of the TickCount (if the timer would have been fired 1000 times in 1 second truly, then the difference of the TickCounts would be 1 each time, thus meaning there's no delay, but if the difference is more than 1, then there is some delay between each times the timer's tick event gets fired).

代码如下:

public partial class Form1 : Form
{
    int localTime = 0, systemTime = 0, baseSystemTime = 0, lastSystemTime = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        // Calculate time based on TickCount
        if (baseSystemTime == 0)
            baseSystemTime = Environment.TickCount;

        systemTime = Environment.TickCount - baseSystemTime;
        label2.Text ="System Time: " + ((systemTime / 1000) / 60).ToString() + ":" + ((systemTime / 1000) % 60).ToString();

        // Calculate time based on timer1_Tick
        localTime++;
        label1.Text = "Application Time: " + ((localTime / 1000) / 60).ToString() + ":" + ((localTime / 1000) % 60).ToString();

        // Calculate the delay
        if (lastSystemTime > 0)
        {
            label3.Text = "Delay: " + (Environment.TickCount - lastSystemTime).ToString() + " ms";
        }

        lastSystemTime = Environment.TickCount;
    }
}

我还在此处上传了整个解决方案: http://ramt.in/test/TimerDelay .zip

I've also uploaded the whole solution here: http://ramt.in/test/TimerDelay.zip

以下是应用程序的屏幕截图(延迟15毫秒,应用程序计算了1秒,而实际上已经过去了17秒!):

Here's a screenshot of the application (with 15 milliseconds delay and 1 second counted by application while 17 seconds have actually passed!):

该解决方案只有50kb,请随时下载并运行该解决方案,以查看是否获得与我相同的结果.如果相同,则Microsoft世界中的计时器类有问题!

The solution is only 50kb, so feel free to download it and run it to see if you get the same result as me. If it's the same, then there's something wrong with the timer class in Microsoft world!

但更重要的是,如果有人对造成这种延迟的原因一窍不通,请与我分享您的知识.

But more importantly, if anyone knows anything about what might cause this delay, please share your knowledge with me.

推荐答案

这是系统问题,不是c#或VB.要检查系统玩具的准确性如何,可以使用Stopwatch类和两个属性

This is a system problem not c# or VB. To check how accurate is your system toy can use Stopwatch class and two properties

  1. IsHighResolution-The timer used by the Stopwatch class depends on the system hardware and operating system. IsHighResolution is true if the Stopwatch timer is based on a high-resolution performance counter. Otherwise, IsHighResolution is false, which indicates that the Stopwatch timer is based on the system timer.
  2. 频率

来自

更新

您在代码中也犯了一个错误:

You also did an error in your code:

        // Calculate time based on timer1_Tick
        localTime++;
        label1.Text = "Application Time: " + ((localTime / 1000) / 60).ToString() + ":" + ((localTime / 1000) % 60).ToString();

这行没有时间过去.它仅计算timer1_Tick运行了多少次. 1毫秒的间隔对于Windows Forms计时器来说太小了.您可以在此处阅读以下内容:计时器花费的时间比间隔多10毫秒

This lines has nothing to time which passed. It only calculates how many times timer1_Tick was run. The 1 millisecond interval is just too small for windows forms timer. You can read about this here: Timer takes 10 ms more than interval

如果您需要更精确的计时器,可以查看本文毫秒和毫秒C#计时器

If you need more precise timer you could look at this article Microsecond and Millisecond C# Timer

这篇关于计时器触发滴答声事件,延迟时间为15毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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