How to get ticks from QueryPerformanceCounter in C#?

后端 未结 6 1081
不知归路
不知归路 2020-12-21 01:22

I need to replace Stopwatch to avoid using getters for its properties. I am going to implement it using QueryPerformanceCounter. I only need ticks nothing e

6条回答
  •  借酒劲吻你
    2020-12-21 01:43

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
    
    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency(out long lpFrequency);
    

    Taken from http://www.codeproject.com/Articles/2635/High-Performance-Timer-in-C

    Old but it should still work

    EDIT: The internals of StopWatch actually use QueryPerformanceCounter, so using the managed code should provide identical results, with better compatibility.

提交回复
热议问题