how to set timer resolution from C# to 1 ms?

前端 未结 2 1503
天涯浪人
天涯浪人 2020-11-29 11:30

I\'ve used this tool and noticed that my Windows Server 2008 R2 Standard has a 15 ms resolution while Windows 8 has a 1 ms resolution timer.

I would prefer to set th

2条回答
  •  余生分开走
    2020-11-29 12:03

    You can try this:

    public static class WinApi
    {
        /// TimeBeginPeriod(). See the Windows API documentation for details.
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
        [DllImport("winmm.dll", EntryPoint="timeBeginPeriod", SetLastError=true)]
    
        public static extern uint TimeBeginPeriod(uint uMilliseconds);
    
        /// TimeEndPeriod(). See the Windows API documentation for details.
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
        [DllImport("winmm.dll", EntryPoint="timeEndPeriod", SetLastError=true)]
    
        public static extern uint TimeEndPeriod(uint uMilliseconds);
    }
    

    And use it like this:

    WinApi.TimeBeginPeriod(1);
    

    And to go back to how it was:

    WinApi.TimeEndPeriod(1);
    

提交回复
热议问题