做一个更准确的计时器.c#中的计时器存在于c++中
Does a more accurate Timers.Timer from c# exists in c++?
我在c#中有一个计时器,每10毫秒执行一个线程:
private void setTimers(IntPtr paramA, List<int> paramB)
{
varTimer= new System.Timers.Timer();
varTimer.Elapsed += delegate { Check(paramA, paramB); };
varTimer.Interval = 10;
varTimer.Start();
}
功能:
private void Check(IntPtr paramA, List<int> paramB)
{
try
{
acquiredLock= false;
System.Threading.Monitor.TryEnter(syncThreadsC, ref acquiredLock);
if (acquiredLock)
{
// logic
}
else
{
return;
}
}
finally
{
if (acquiredLock)
System.Threading.Monitor.Exit(syncThreadsC);
}
}
我要求它每10毫秒启动一次,即使知道它不会非常精确,当我进入和退出Check
时,如果我检查StopWatch,我会得到这些数字:
[605] [668] [693] [755] [785] [847] [878] [941] [971] [40] [67] [128][159]
Check
的逻辑只需要1到2 ms。我想知道,因为我相信c#中没有什么比这更精确的了,如果c++中有任何东西可以每X毫秒以更高的精度启动一个计时器。如果是这样,我将在一个外部dll中调用它。这些数字离我需要的太远了。
谢谢
在高分辨率interval/timer中使用Raise事件的正确计时器类,这里是一个高分辨率计时器的示例实现:
class Program
{
public void Main(string[] args)
{
uint timerId = SafeNativeMethods.timeSetEvent( 1, 1, HandleTimerTick, UIntPtr.Zero, 1 );
Console.ReadLine();
SafeNativeMethods.timeKillEvent( timerId );
}
public static void HandleTimerTick( uint id, uint msg, UIntPtr userCtx, UIntPtr uIntPtr, UIntPtr intPtr )
{
Console.WriteLine( "This is a bad idea on short timescales" );
}
}
public static class SafeNativeMethods
{
/// <summary>
/// A timer event handler
/// </summary>
/// <param name="id">Timer identifier, as returned by the timeSetEvent function.</param>
/// <param name="msg">Reserved</param>
/// <param name="userCtx">The value that was passed in to the userCtx value of the timeSetEvent function.</param>
/// <param name="dw1">Reserved</param>
/// <param name="dw2">Reserved</param>
public delegate void TimerEventHandler( UInt32 id, UInt32 msg, UIntPtr userCtx, UIntPtr dw1, UIntPtr dw2 );
/// <summary>
/// A multi media timer with millisecond precision
/// </summary>
/// <param name="msDelay">One event every msDelay milliseconds</param>
/// <param name="msResolution">Timer precision indication (lower value is more precise but resource unfriendly)</param>
/// <param name="handler">delegate to start</param>
/// <param name="userCtx">callBack data </param>
/// <param name="eventType">one event or multiple events</param>
/// <remarks>Dont forget to call timeKillEvent!</remarks>
/// <returns>0 on failure or any other value as a timer id to use for timeKillEvent</returns>
[DllImport( "winmm.dll", SetLastError = true, EntryPoint = "timeSetEvent" )]
public static extern UInt32 timeSetEvent( UInt32 msDelay, UInt32 msResolution, TimerEventHandler handler, UIntPtr userCtx, UInt32 eventType );
/// <summary>
/// The multi media timer stop function
/// </summary>
/// <param name="uTimerID">timer id from timeSetEvent</param>
/// <remarks>This function stops the timer</remarks>
[DllImport( "winmm.dll", SetLastError = true )]
public static extern void timeKillEvent( UInt32 uTimerID );
}
请确保在你用完计时器后干净利落地终止它,使用timeKillEvent,请不要在你的应用程序中使用大量这样的计时器。
相关文章:
- Linux的Cpp上的计时器
- 提升 ASIO 无法识别计时器对象
- 提升 asio 并发计时器取消问题与链
- 使用单体计时器的pthread_cond_timedwait有时会比预期晚超时
- 窗口中的微秒计时器
- 计时器是否从另一个线程启动?
- 如何在 c++ 中创建计时器
- C++回调计时器实现
- 在计时器或主线程外部的命令上销毁/替换线程
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 保留计时器集合(对象与指针)的最佳方法
- 在网络套接字计时器滴答后增加asio短读错误
- 是否可以仅使用标准 c++/c++11 实现不带"sleep"的计时器?
- 在没有NtSetTimerResolution的Windows上提高计时器分辨率(高分辨率)
- 计时器坏了或者其他什么的
- 功能计时器阻止主功能继续
- IO服务重新启动后,Boost最后期限计时器持续触发
- boost asio计时器是否会在"取消"时阻塞
- Poco 计时器,具有来自同一类的回调
- 使计时器在C++进程中的特定时间关闭,以便同步两个进程