Windows API 中提供了哪些周期性计时器对象?
在研究比 .NET 计时器对象提供的 15 毫秒分辨率更好的选项时(请参阅 https://stackoverflow.com/questions/3744032/why-are-net-timers-limited-to-15-ms-resolution),我正在查看不同的计时器对象Windows 提供。我已确定以下内容:
Windows API 中是否还有其他可用的计时器类型?
In researching options for getting better than the 15 ms resolution provided by .NET timer objects (see https://stackoverflow.com/questions/3744032/why-are-net-timers-limited-to-15-ms-resolution), I am looking at the different timer objects that Windows provides. I have identified the following:
Are there other timer types available in the Windows API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我问这个问题是因为我有兴趣实现一个周期性计时器,它比 .NET 提供的计时器具有更好的粒度。我对这些计时器(
Windows.Forms.Timer
、System.Timers.Timer
和System.Threading.Timer
)的调查表明,最好的我希望粒度为 15 毫秒,精度为 -1 到 +30 毫秒。这对于大多数应用程序来说都很好,但对于我正在开发的应用程序来说则不然。有关我的调查的详细信息,请参阅 为什么 .NET 计时器仅限于15 毫秒分辨率? 和 http://www .informit.com/guides/content.aspx?g=dotnet&seqNum=815。
这促使我寻找 Windows 下可用的周期性计时器对象。我确定了在原始问题中发布的五种类型。我没有找到其他人。我放弃了旧式 Windows 计时器,因为我不想处理消息。然后,我用 C# 为其他四种计时器类型编写了托管原型并进行了一些测试。
所有四种计时器类型(多媒体计时器、可等待计时器、计时器队列计时器和线程池计时器)都提供可靠的 1 毫秒间隔,具有非常好的分辨率。其中,线程池计时器是迄今为止最容易交互的,但不幸的是 Windows XP 不支持它。计时器队列计时器有一系列令人眼花缭乱的选项,但如果您忽略大多数选项,它们几乎与线程池计时器一样简单。有关详细信息,请参阅探索更好的计时器选项。
我选择在 .NET 中为我的通用计时器类包装计时器队列计时器。您可以在此处查看。
您可能还对 .NET 中的等待计时器感兴趣使用 C#。
I asked this question because I was interested in implementing a periodic timer that would give me better granularity than the timers that are supplied with .NET. My investigation of those timers (
Windows.Forms.Timer
,System.Timers.Timer
, andSystem.Threading.Timer
) shows that the best I can hope for is 15 ms granularity, and accuracy of -1 to +30 ms. That's fine for most applications, but not for the application I was working on.For details of my investigation, see Why are .NET timers limited to 15 ms resolution? and http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=815.
That led me to looking for periodic timer objects available under Windows. I identified the five types that I posted in the original question. I didn't find any others. I discarded the old-style Windows timer because I don't want to be processing messages. I then wrote managed prototypes in C# for the other four timer types and did some testing.
All four timer types (Multimedia timers, Waitable timers, Timer queue timers, and Threadpool timers) give a reliable 1 ms interval, with very good resolution. Of those, the Threadpool timer is by far the easiest to interact with, but unfortunately it's not supported on Windows XP. Timer queue timers have a dizzying array of options, but if you ignore most of the options they're almost as simple as Threadpool timers. See Exploring options for better timers for more info.
I chose to wrap the timer queue timer for my general timer class in .NET. You can see it here.
You might also be interested in Waitable Timers in .NET with C#.