Как получить простой системы в миллисекундах - PullRequest
1 голос
/ 11 января 2011

Я использовал это в течение долгого времени, но теперь я обнаружил ошибку, которая после 48 дней не дает мне правильное время простоя.

Это связано с целыми 32 битами.Мне удалось получить счетчик тиков в течение длительного времени через Environment.TickCount недостаточно

Однако мне нужно получить время простоя, и GetLastInputTime () [http://www.codeproject.com/KB/system/rtwidledll.aspx?display=PrintAll]не вернет меня долго.

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;
}
...