首页 > 系统相关 >Windows高精度计时器(适用短且占用性能的方式)

Windows高精度计时器(适用短且占用性能的方式)

时间:2024-12-12 09:56:21浏览次数:6  
标签:QueryPerformanceCounter Windows 高精度 long 短且 frequency 计时器 out

QueryPerformanceCounter:获取当前的高精度计时器值
QueryPerformanceFrequency:获取高精度计时器的频率

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency); 
// 高精度睡眠函数
 static void HighPrecisionSleep(int milliseconds)
 {
     long start = 0, stop = 0, frequency = 0;
     QueryPerformanceFrequency(out frequency);
     QueryPerformanceCounter(out start);

     long targetTicks = (frequency / 1000) * milliseconds;

     do
     {
         QueryPerformanceCounter(out stop);
     } while ((stop - start) < targetTicks);
 }

  相当于 while(true) 到时了break;

标签:QueryPerformanceCounter,Windows,高精度,long,短且,frequency,计时器,out
From: https://www.cnblogs.com/stweily/p/18601624

相关文章