为什么尽管timeBeginPeriod最小Threading.Timer间隔15毫秒(1)

编程入门 行业动态 更新时间:2024-10-08 08:26:40
本文介绍了为什么尽管timeBeginPeriod最小Threading.Timer间隔15毫秒(1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的代码片段

[DllImport("winmm.dll", EntryPoint = "timeBeginPeriod")] public static extern uint TimeBeginPeriod(uint uMilliseconds); static void Main(string[] args) { if (TimeBeginPeriod(1) != 0) Console.WriteLine("TimeBeginPeriod failed!"); Console.WriteLine("Sleep"); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < 10; i++) { Thread.Sleep(1); Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency); sw.Restart(); } Console.WriteLine("Threading.Timer"); sw = null; System.Threading.Timer t = null; int n = 0; t = new Timer(state => { if (sw == null) sw = Stopwatch.StartNew(); else { Console.WriteLine(sw.ElapsedTicks * 1000d / Stopwatch.Frequency); n++; sw.Restart(); } if (n == 10) t.Change(Timeout.Infinite, Timeout.Infinite); }, null, TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(1)); Console.ReadKey(); }

会产生如这样的输出:

will produce e.g. this output:

Sleep 0.151834939915548 0.757358826331279 0.786901687225611 0.712520725399457 0.715593741662697 0.798704863327602 0.5724889615859 0.648825479215934 0.436927039609783 0.517873081634677 Threading.Timer 15.5841035662354 14.8620145856526 15.1098812837944 14.4202684978119 15.3883384620112 14.7210748852159 15.307462261265 15.7125416777831 14.5991320125882 15.6035194417168

<据网,例如汉斯帕桑特一个评论, timeBeginPeriod 影响规律(.NET )定时器。那么,为什么我的定时器仍然有这个粗粒度? Thread.sleep代码似乎做就好了。

According to the net, e.g. a comment by Hans Passant, timeBeginPeriod affects the regular () timers. So why does my timer still have this coarse granularity? Thread.Sleep seems to do just fine.

也许相关:这个运行在Windows 7中,64位,.NET 4中,里面的VMWare

Maybe relevant: This runs on Windows 7, 64bit, 4, inside VMWare.

推荐答案

注释是错误的。我的经验是,多媒体计时器不会影响.NET定时器。也就是说,它不改变其支持的最低时间周期,这似乎是大约15毫秒。它的也许的提高其准确性。也就是说,如果你问16毫秒,你实际上可能得​​到16毫秒,而不是地方15到30毫秒之间。

The comment is wrong. My experience has been that the multimedia timer does not affect the .NET timers. That is, it doesn't change their minimum supported time period, which appears to be about 15 ms. It might improve their accuracy. That is, if you ask for 16 ms, you might actually get 16 ms and not "somewhere between 15 and 30 ms."

为什么.NET定时器被限制在15毫秒的我也不清楚。

Why the .NET timers are limited to 15 ms is not clear to me.

有关于它的接受的答案一些信息。

There's some information about it in the accepted answer here.

如果你正在寻找一个更高的分辨率计时器.NET,你可能不应该使用的多媒体计时器。那些已经pcated Windows API中去$ P $。使用计时器队列计时器。请参阅我的文章:

If you're looking for a higher resolution timer for .NET, you probably shouldn't use the multimedia timers. Those have been deprecated in the Windows API. Use the Timer Queue Timers. See my articles:

  • .NET计时器分辨率
  • 探索更好的定时器选项
  • 使用Windows定时器队列API
  • .NET Timer Resolution
  • Exploring options for better timers
  • Using the Windows Timer Queue API

另一种选择是使用 waitable计时器。全部源代码是可以在 www.mischel/pubs/waitabletimer.zip

Another option is to use the Waitable Timer. Full source for that is available at www.mischel/pubs/waitabletimer.zip

更多推荐

为什么尽管timeBeginPeriod最小Threading.Timer间隔15毫秒(1)

本文发布于:2023-05-31 13:59:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/393014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   最小   timeBeginPeriod   Threading   Timer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!