首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >来自c#的更精确的c#是否存在于c++中?

来自c#的更精确的c#是否存在于c++中?
EN

Stack Overflow用户
提问于 2015-08-12 01:59:14
回答 1查看 609关注 0票数 2

我在c#中有一个计时器,每10 in执行一次线程:

代码语言:javascript
运行
复制
        private void setTimers(IntPtr paramA, List<int> paramB)
    {
        varTimer= new System.Timers.Timer();
        varTimer.Elapsed += delegate { Check(paramA, paramB); };
        varTimer.Interval = 10;
        varTimer.Start();
    }

职能如下:

代码语言:javascript
运行
复制
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时,如果我向Check查询的话,我会得到这些数字

605 693 785 878 971 67 159

Check的逻辑只需1到2ms。我想知道,因为我相信在c#中没有比这更精确的东西了,如果c++中有什么东西可以以更高的精度在每一个X毫秒启动一个定时器的话。如果是这样的话,我将在外部dll中这样做并调用它。这些数字对我来说太不合适了。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-12 03:02:16

使用来自Raise event in high resolution interval/timer的校正计时器类,下面是一个高分辨率定时器的示例实现:

代码语言:javascript
运行
复制
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时,请确保干净地杀死您的计时器,并且不要在您的应用程序中使用大量这些计时器。它们是资源密集型的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31954828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档