c# - Environment.TickCount 是不够的

标签 c# .net c#-4.0 environment-variables gettickcount

我想知道系统最后一次启动是什么时候。

Environment.TickCount 可以工作,但由于 int 的限制,它会在 48-49 天后中断。

这是我一直在使用的代码:

Environment.TickCount & Int32.MaxValue

有人知道 long type return 吗?

我用它来了解系统的空闲时间:

public static int GetIdleTime()
{
    return (Environment.TickCount & Int32.MaxValue)- (int)GetLastInputTime();
}

/// <summary>
/// Get the last input time from the input devices.
/// Exception: 
/// If it cannot get the last input information then it throws an exception with 
/// the appropriate message.
/// </summary>
/// <returns>Last input time in milliseconds.</returns>
public static uint GetLastInputTime()
{
    LastInputInfo lastInPut = new LastInputInfo();
    lastInPut.BlockSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
    if (!GetLastInputInfo(ref lastInPut))
    {
        throw new Exception(GetLastError().ToString());
    }

    return lastInPut.Time;
}

最佳答案

以下代码检索自系统启动以来的毫秒数(调用 unmanged API)。我测量了该互操作操作的性能成本,它与 StopWatch() 完全相同(当然,它不会检索自系统直接启动以来的时间)。

using System.Runtime.InteropServices;

...

[DllImport("kernel32.dll") ]
public static extern UInt64 GetTickCount64();

...

var tickCount64 = GetTickCount64();

https://msdn.microsoft.com/de-de/library/windows/desktop/ms724411(v=vs.85).aspx

关于c# - Environment.TickCount 是不够的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645171/

相关文章:

c# - 为什么代码分析选项卡不显示在我的 Visual Studio 2010 Premium 项目属性中?

.net - 使用 .Net 库将 CSV 文件上传到 Google Cloud Storage

c# - 为什么这段代码不会以死锁结束

asp.net-mvc - VS 2012 中缺少 "Browse With..."按钮

c# - 在移动设备中实现 Google map

c# - 很好地处理空值的语法

c# - 如何在服务中为 Blazor 客户端应用程序调用 HttpClient

c# - ILMerge:如何合并Azure运行时库?

.net - 为什么 Control.Invoke 是必要的?

.net - 领域驱动设计示例(特别是 .NET 重点)