Timer takes 10 ms more than interval

2019-01-18 16:31发布

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}

标签: c# .net timer
11条回答
啃猪蹄的小仙女
2楼-- · 2019-01-18 16:55

As other responders have mentioned, Windows is not a real-time OS. If you must use windows, try using Win CE or Windows Embedded.

-S!

查看更多
smile是对你的礼貌
3楼-- · 2019-01-18 16:58

The accuracy of the time may depend on how many processes run. If you have that option , I would reduce the number of processes that run on your computer one by one and I mean those which consume significant cpu time,I would check if the times improve. Especially, browsers, virus scanners,programs running in the background.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-18 16:59

On my system it's 14ms. Having googled; the difference is down to context thread switching delay. There's an article regarding high resolution timers here

查看更多
叛逆
5楼-- · 2019-01-18 17:06

First, as other people have noted, you're setting it to 1s, not 50ms.

Secondly, windows is not a real-time OS. None of the timer classes are exactly precise. All you're doing it saying that you want to wait at least this long. It takes some amount of time for everything to fire and you to end up notified that the timer has ticked once windows gets around to actually servicing the tick message.

查看更多
Root(大扎)
6楼-- · 2019-01-18 17:07

The deviations are normal since they are not RTOS (real time operating systems). This is the best solution that I've found under the circumstances: Link

Program.MicroTimer microTimer = new Program.MicroTimer();
microTimer.MicroTimerElapsed += new Program.MicroTimer.MicroTimerElapsedEventHandler(OnTimedEvent);
microTimer.Interval = 1000; // Call micro timer every 1000µs (1ms)

// Can choose to ignore event if late by Xµs (by default will try to catch up)
// microTimer.IgnoreEventIfLateBy = 500; // 500µs (0.5ms)

microTimer.Enabled = true; // Start timer
System.Threading.Thread.Sleep(2000);
microTimer.Enabled = false;

Those are the code snippets. You can try them to see the values in the console.

查看更多
Luminary・发光体
7楼-- · 2019-01-18 17:10

If you need a more precise timer, you can hook into the Win32 Multimedia Timer, it is the most accurate timer (down to 1ms). Here's an article on CodeProject showing how to hook into it from C#.

查看更多
登录 后发表回答