| | 1 | | using System.Runtime.InteropServices; |
| | 2 | | using System.Threading; |
| | 3 | |
|
| | 4 | | namespace Imagini.Core.Internal |
| | 5 | | { |
| | 6 | | public static class Native |
| | 7 | | { |
| | 8 | | public static class Windows |
| | 9 | | { |
| | 10 | | [DllImport("ntdll.dll", SetLastError = true)] |
| | 11 | | private static extern int NtQueryTimerResolution(out uint MinimumResolution, out uint MaximumResolution, out uint |
| | 12 | |
|
| | 13 | | private static readonly double threshold; |
| | 14 | |
|
| | 15 | | static Windows() |
| | 16 | | { |
| 0 | 17 | | NtQueryTimerResolution(out uint min, out uint max, out uint current); |
| 0 | 18 | | threshold = max * 0.0001 + 1; |
| 0 | 19 | | } |
| | 20 | |
|
| | 21 | | private static double QueryTimerResolution() |
| | 22 | | { |
| 0 | 23 | | NtQueryTimerResolution(out uint min, out uint max, out uint current); |
| 0 | 24 | | return current * 0.0001; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public static void SleepAtMost(double ms) |
| | 28 | | { |
| 0 | 29 | | if (ms < threshold) |
| 0 | 30 | | return; |
| 0 | 31 | | var sleepTime = (int)(ms - QueryTimerResolution()); |
| 0 | 32 | | if (sleepTime > 0) |
| 0 | 33 | | Thread.Sleep(sleepTime); |
| 0 | 34 | | } |
| | 35 | | } |
| | 36 | | } |
| | 37 | | } |