58 lines
1.4 KiB
C#
58 lines
1.4 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace TimerApp
|
|
{
|
|
public static class NativeMethods
|
|
{
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
struct LASTINPUTINFO
|
|
{
|
|
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
|
|
|
|
[MarshalAs(UnmanagedType.U4)]
|
|
public UInt32 cbSize;
|
|
[MarshalAs(UnmanagedType.U4)]
|
|
public UInt32 dwTime;
|
|
}
|
|
|
|
[DllImport("user32.dll")]
|
|
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
|
|
|
|
[DllImport("kernel32.dll")]
|
|
static extern uint GetTickCount();
|
|
|
|
/// <summary>
|
|
/// 获取系统空闲时间(毫秒)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static long GetIdleTime()
|
|
{
|
|
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO
|
|
{
|
|
cbSize = (uint)Marshal.SizeOf<LASTINPUTINFO>(),
|
|
dwTime = 0
|
|
};
|
|
|
|
if (GetLastInputInfo(ref lastInputInfo))
|
|
{
|
|
uint tickCount = GetTickCount();
|
|
uint idleMs = unchecked(tickCount - lastInputInfo.dwTime);
|
|
return idleMs;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static NativeMethods()
|
|
{
|
|
}
|
|
|
|
public static void Shutdown()
|
|
{
|
|
}
|
|
}
|
|
}
|