亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以高分辨率間隔/計時器引發事件

以高分辨率間隔/計時器引發事件

C#
達令說 2019-12-04 15:00:43
我想通過c#使用盡可能高的分辨率計時器。例如,我想每11個滴答聲觸發一個事件(我聽說滴答聲是pc中可能的最高計數器)。我嘗試使用計時器,發現最小經過的時間以毫秒為單位。我看著秒表,但秒表沒有引發事件。謝謝。
查看完整描述

3 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

使用多媒體計時器應該可以為您每秒帶來約1000個事件。此代碼應在途中為您提供幫助。


     public delegate void TimerEventHandler(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2);


    /// <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")]

    static extern UInt32 timeSetEvent(UInt32 msDelay, UInt32 msResolution, TimerEventHandler handler, ref UInt32 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)]

    static extern void timeKillEvent(  UInt32 uTimerID );

運行它們后,請停止它們。它們在您的系統上非常繁重*。捕獲所有異常,不要讓它們逃離事件處理程序。


*啟動超過5個計時器將嚴重降低大多數系統的速度!在事件處理程序中執行盡可能少的代碼,并確保執行的代碼快于1毫秒,否則會遇到嚴重的問題。我每隔10至50個滴答聲啟動一個代表,以增加標簽顯示。


發生在a上的普通線程切換Thread.Sleep將使您的代碼中沒有一個線程插槽,大約需要40毫秒。您還可以通過一些NT內核調用來增加線程切換頻率,但是請不要這樣做。


查看完整回答
反對 回復 2019-12-04
  • 3 回答
  • 0 關注
  • 614 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號