Skip to content

Commit de9fb5f

Browse files
committed
#32 自动锁定延迟启动
1 parent 132ac22 commit de9fb5f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/ComputerLock/Platforms/UserActivityMonitor.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class UserActivityMonitor : IDisposable
1111
private bool _isMonitoring;
1212
private readonly Dispatcher _dispatcher;
1313
private bool _disposed;
14+
// 记录开始监控的时间,用来延迟解锁后的检测空闲
15+
private long _lastStartTime;
1416

1517
public event EventHandler? OnIdle;
1618

@@ -35,6 +37,7 @@ public void StartMonitoring()
3537
RunOnUIThread(() =>
3638
{
3739
_isMonitoring = true;
40+
_lastStartTime = Environment.TickCount64;
3841
});
3942
}
4043

@@ -53,12 +56,21 @@ private void Timer_Tick(object? sender, EventArgs e)
5356
return;
5457
}
5558

59+
// 检查是否已经监控了足够长的时间(至少2秒)
60+
long currentTime = Environment.TickCount64;
61+
long monitoringDuration = currentTime - _lastStartTime;
62+
if (monitoringDuration < 5000)
63+
{
64+
// 重新启动监控时,延迟 5 秒后再开始检测空闲
65+
return;
66+
}
67+
5668
var lastInputInfo = new WinApi.LastInputInfo();
5769
lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
5870

5971
if (WinApi.GetLastInputInfo(ref lastInputInfo))
6072
{
61-
long elapsedMillisecond = Environment.TickCount64 - lastInputInfo.dwTime;
73+
long elapsedMillisecond = currentTime - lastInputInfo.dwTime;
6274
if (elapsedMillisecond > _autoLockMillisecond)
6375
{
6476
OnIdle?.Invoke(this, EventArgs.Empty);

0 commit comments

Comments
 (0)