我有一个控制台应用程序的以下代码片段,如果空闲时间超过 5 秒,我想锁定我的计算机。
问题:5 秒后根本没有任何反应
public static uint GetIdleTime()
{
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
GetLastInputInfo(ref lastInPut);
return ((uint)Environment.TickCount - lastInPut.dwTime);
}
public static long GetTickCount()
{
return Environment.TickCount;
}
public static long GetLastInputTime()
{
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
if (!GetLastInputInfo(ref lastInPut))
{
throw new Exception(GetLastError().ToString());
}
return lastInPut.dwTime;
}
private static Timer timer;
private static uint idle = 0;
static void Main(string[] args)
{
timer = new Timer();
timer.Enabled = true;
timer.Interval = 1000;
timer.Start();
idle += GetIdleTime();
System.Threading.Thread.Sleep(6000);
if (idle > 5000)
{
LockWorkStation();
}
}