高精度时间戳getter,返回毫秒为Int64

本文关键字:Int64 返回 时间戳 getter 高精度 | 更新日期: 2023-09-27 17:54:11

需要一个高精度的。net时间戳函数

我已经使用了winapi TimeGetTime。但使用Int32,每49.8天循环一次,可返回signed

返回类型为Int64UInt64,返回毫秒的本机。net高精度函数是什么?

从系统开始还是从过去的任何时间返回毫秒数并不重要。

实际上我不需要StopWatch

编辑:


我的应用程序是多线程的。应该为每个线程创建StopWatch实例。StopWatch具有非常高精度的计时器。我只需要1-5毫秒。我需要的是timeGetTime/Environment之类的东西。TickCount作为Int64或DateTime。更新分辨率为1-5ms的刻度

高精度时间戳getter,返回毫秒为Int64

如果你是Windows 8或Server 2012或更高版本,使用GetSystemTimePreciseAsFileTime如下:

[DllImport("Kernel32.dll", CallingConvention = CallingConvention.Winapi)]
static extern void GetSystemTimePreciseAsFileTime(out long filetime);
public DateTimeOffset GetNow()
{
    long fileTime;
    GetSystemTimePreciseAsFileTime(out fileTime);
    return DateTimeOffset.FromFileTime(fileTime);
}

这比DateTime.UtcNow.Ticks的准确率要高得多。

查看MSDN获取更多信息:http://msdn.microsoft.com/en-us/library/windows/desktop/hh706895(v=vs.85).aspx

您可以使用DateTime.UtcNow.Ticks。这实际上比毫秒更精确