Get time in milliseconds using C#

后端 未结 8 1631
你的背包
你的背包 2021-01-30 09:56

I\'m making a program in which I need to get the time in milliseconds. By time, I mean a number that is never equal to itself, and is always 1000 numbers bigger than it was a se

8条回答
  •  生来不讨喜
    2021-01-30 10:34

    You can try the QueryPerformanceCounter native method. See http://www.pinvoke.net/default.aspx/kernel32/QueryPerformanceCounter.html for more information. This is what the Stopwatch class uses.

    See How to get timestamp of tick precision in .NET / C#? for more information.

    Stopwatch.GetTimestamp() gives access to this method:

    public static long GetTimestamp() {
         if(IsHighResolution) {
             long timestamp = 0;
             SafeNativeMethods.QueryPerformanceCounter(out timestamp);
             return timestamp;
         }
         else {
             return DateTime.UtcNow.Ticks;
         }
     }
    

提交回复
热议问题