准确的 Windows 计时器?System.Timers.Timer() 限制为 15 毫秒 [英] Accurate Windows timer? System.Timers.Timer() is limited to 15 msec

查看:25
本文介绍了准确的 Windows 计时器?System.Timers.Timer() 限制为 15 毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个准确的计时器来将 Windows 应用程序连接到实验室设备.

I need an accurate timer to interface a Windows application to a piece of lab equipment.

我使用 System.Timers.Timer() 创建了一个每 10 毫秒滴答一次的计时器,但是这个时钟运行缓慢.例如,间隔为 10 毫秒的 1000 个滴答声应该需要 10 秒,但实际上需要更像是 20 秒(在我的 PC 上).我猜这是因为 System.Timers.Timer() 是一个间隔计时器,每次经过时都会重置.由于在计时器结束和重置(另一个 10 毫秒)之间总是需要一些时间,因此时钟将运行缓慢.如果间隔很大(几秒或几分钟),这可能没问题,但对于很短的间隔是不可接受的.

I used System.Timers.Timer() to create a timer that ticks every 10 msec, but this clock runs slow. For example 1000 ticks with an interval of 10 msec should take 10 wall-clock seconds, but it actually takes more like 20 wall-clock sec (on my PC). I am guessing this is because System.Timers.Timer() is an interval timer that is reset every time it elapses. Since it will always take some time between when the timer elapses and when it is reset (to another 10msec) the clock will run slow. This probably fine if the interval is large (seconds or minutes) but unacceptable for very short intervals.

Windows 上是否有一个函数可以在每次系统时钟跨越 10 毫秒(或其他)边界时触发一个过程?

Is there a function on Windows that will trigger a procedure every time the system clock crosses a 10 msec (or whatever) boundary?

这是一个简单的控制台应用程序.

This is a simple console application.

谢谢

规范

更新:System.Timers.Timer() 对于小间隔非常不准确.

UPDATE: System.Timers.Timer() is extremely inaccurate for small intervals.

我编写了一个简单的程序,它以多种方式计算 10 秒:

I wrote a simple program that counted 10 seconds several ways:

间隔=1,计数=10000,运行时间=160 秒,每间隔毫秒=16

Interval=1, Count=10000, Run time = 160 sec, msec per interval=16

间隔=10,计数=1000,运行时间=16 秒,每间隔毫秒=15

Interval=10, Count=1000, Run time = 16 sec, msec per interval=15

间隔=100,计数=100,运行时间=11 秒,每间隔毫秒=110

Interval=100, Count=100, Run time = 11 sec, msec per interval=110

间隔=1000,计数=10,运行时间=10 秒,每间隔毫秒=1000

Interval=1000, Count=10, Run time = 10 sec, msec per interval=1000

似乎 System.Timers.Timer() 不能比大约 15 毫秒更快,无论如何间隔设置.

It seems like System.Timers.Timer() cannot tick faster that about 15 msec, regardless of the interval setting.

请注意,这些测试似乎都没有使用任何可测量的 CPU 时间,因此限制不是 CPU,只是 .net 限制(错误?)

Note that none of these tests seemed to use any measurable CPU time, so the limit is not the CPU, just a .net limitation (bug?)

现在我认为我可以忍受一个不准确的计时器,它每 15 毫秒左右触发一次例程,并且例程获得准确的系统时间.有点奇怪,但是...

For now I think I can live with an inaccurate timer that triggers a routine every 15 msec or so and the routine gets an accurate system time. Kinda strange, but...

我还发现了一个共享软件产品 ZylTimer.NET,它声称是一个更准确的 .net 计时器(分辨率为 1-2 毫秒).这可能是我需要的.如果只有一种产品,可能还有其他产品.

I also found a shareware product ZylTimer.NET that claims to be a much more accurate .net timer (resolution of 1-2 msec). This may be what I need. If there is one product there are likely others.

再次感谢.

推荐答案

您需要使用 高分辨率计时器,例如 QueryPerformanceCounter

You need to use a high resolution timer such as QueryPerformanceCounter

这篇关于准确的 Windows 计时器?System.Timers.Timer() 限制为 15 毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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