.net框架有很多种方法可以使用计时器,但对于节拍器我应该使用哪一个?
我尝试了所有这些计时器,但没有一个对我的使用来说足够准确(他们无法在15ms以下获得分辨率)。
System.Windows.Forms.Timer System.Web.UI.Timer System.Diagnostics.Stopwatch System.Timers.Timer System.Threading.Timer对于我的节拍器课程,我需要Ticks可靠,恒定,无漂移。
我在MSDN上找到了这个实现: https ://msdn.microsoft.com/en-us/library/aa964692( v = vs。85).aspx使用“coredll.dll”
另一种解决方案是实现这些本机方法:
[DllImport("winmm.dll", SetLastError = true, EntryPoint = "timeSetEvent")] internal static extern UInt32 TimeSetEvent(UInt32 msDelay, UInt32 msResolution, MultimediaTimerCallback callback, ref UInt32 userCtx, UInt32 eventType); [DllImport("winmm.dll", SetLastError = true, EntryPoint = "timeKillEvent")] internal static extern void TimeKillEvent(UInt32 uTimerId);在Windows上拥有分辨率低于5ms的计时器的最佳方法是什么?
编辑
我在.net(法语)中找到了关于计时器的这个非常有趣的链接: http : //www.e-naxos.com/Blog/post/NET-et-ses-timers-Prise-de-tete-ou-reelle- utilite-的.aspx
The .net framework has many ways to use timers, but for a metronome which one should I use ?
I tried all these timers but no one is enough accurate for my use (they can't have a resolution under 15ms).
System.Windows.Forms.Timer System.Web.UI.Timer System.Diagnostics.Stopwatch System.Timers.Timer System.Threading.TimerFor my metronome class I need Ticks reliable , constant, without drift.
I found this implementation on MSDN : https://msdn.microsoft.com/en-us/library/aa964692(v=vs.85).aspx that use "coredll.dll"
Another solution can be to implement these native methods:
[DllImport("winmm.dll", SetLastError = true, EntryPoint = "timeSetEvent")] internal static extern UInt32 TimeSetEvent(UInt32 msDelay, UInt32 msResolution, MultimediaTimerCallback callback, ref UInt32 userCtx, UInt32 eventType); [DllImport("winmm.dll", SetLastError = true, EntryPoint = "timeKillEvent")] internal static extern void TimeKillEvent(UInt32 uTimerId);What is the best way to have a timer with a resolution under 5ms on windows ?
EDIT
I found this very interesting link about timers in .net (in french) : http://www.e-naxos.com/Blog/post/NET-et-ses-timers-Prise-de-tete-ou-reelle-utilite-.aspx
最满意答案
您应该使用多媒体计时器来实现此目的。
多媒体计时器服务允许应用程序以最高分辨率(或准确度)为硬件平台调度计时器事件。 这些多媒体计时器服务允许您以比其他计时器服务更高的分辨率安排计时器事件。
这些定时器服务对于需要高分辨率定时的应用程序非常有用。 例如,MIDI音序器需要高分辨率计时器,因为它必须在1毫秒的分辨率内保持MIDI事件的速度。
有各种C#库可以帮助使用它们。
例如: http : //www.codeproject.com/Articles/5501/The-Multimedia-Timer-for-the-NET-Framework
You should use a Multimedia Timer for this purpose.
Multimedia timer services allow applications to schedule timer events with the greatest resolution (or accuracy) possible for the hardware platform. These multimedia timer services allow you to schedule timer events at a higher resolution than other timer services.
These timer services are useful for applications that demand high-resolution timing. For example, a MIDI sequencer requires a high-resolution timer because it must maintain the pace of MIDI events within a resolution of 1 millisecond.
There are various C# libraries available to help use these.
For example: http://www.codeproject.com/Articles/5501/The-Multimedia-Timer-for-the-NET-Framework
更多推荐
发布评论