This question already has answers here:
Raise event in high resolution interval/timer
(3个答案)
5年前关闭。
我在C#中有一个计时器,每10毫秒执行一次线程:
和功能:
我要求它每10毫秒启动一次,甚至知道它不会非常精确,如果我在进入和退出
谢谢
完成后,请确保使用timeKillEvent彻底清除计时器,请不要在应用程序中使用大量此类计时器。它们是资源密集型的。
(3个答案)
5年前关闭。
我在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进行检查,就会得到这些数字:Check
的逻辑仅在1到2毫秒之间。我想知道,因为我相信C#中没有比它更精确的东西了,所以c++中是否有什么东西可以每X ms启动一个更高精度的计时器。如果是这样,我将在一个外部dll中进行调用。这些数字对于我所需要的来说实在是太少了。谢谢
最佳答案
使用来自Raise event in high resolution interval/timer的更正计时器类,这是高分辨率计时器的示例实现:
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彻底清除计时器,请不要在应用程序中使用大量此类计时器。它们是资源密集型的。
关于c# - c++中是否存在来自C#的更准确的Timers.Timer? [复制],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31954828/
10-09 19:41