From e26c015e0963c4da2117e74673e5a54eea107c33 Mon Sep 17 00:00:00 2001 From: Solin Date: Sat, 17 Jan 2026 13:26:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E4=BC=91=E6=81=AF?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=AE=A1=E6=95=B0=E5=99=A8=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=B7=B3=E5=8F=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ActivityMonitor.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ActivityMonitor.cs b/ActivityMonitor.cs index 65b4791..d544b62 100644 --- a/ActivityMonitor.cs +++ b/ActivityMonitor.cs @@ -16,6 +16,7 @@ namespace TimerApp private DateTime _lastWorkStartTime; private TimeSpan _accumulatedWorkTime; private DateTime _restStartTime; + private int _restElapsedSeconds; // 休息已过秒数(用于避免时间计算导致的跳变) // 配置 (默认值) public TimeSpan WorkDuration { get; set; } = TimeSpan.FromMinutes(20); @@ -73,10 +74,12 @@ namespace TimerApp if (CurrentState == MonitorState.Resting) { // 休息模式逻辑 - TimeSpan timeInRest = DateTime.Now - _restStartTime; - TimeSpan remainingRest = RestDuration - timeInRest; + // 使用计数器而不是时间差,避免秒数跳变 + _restElapsedSeconds++; + int totalRestSeconds = (int)RestDuration.TotalSeconds; + int remainingSeconds = totalRestSeconds - _restElapsedSeconds; - if (remainingRest <= TimeSpan.Zero) + if (remainingSeconds <= 0) { // 休息结束 RestEnded?.Invoke(this, EventArgs.Empty); @@ -84,6 +87,7 @@ namespace TimerApp } else { + TimeSpan remainingRest = TimeSpan.FromSeconds(remainingSeconds); RestProgressChanged?.Invoke(this, remainingRest); } } @@ -130,6 +134,7 @@ namespace TimerApp { // 触发休息 _restStartTime = DateTime.Now; + _restElapsedSeconds = 0; // 重置休息计数器 ChangeState(MonitorState.Resting); RestStarted?.Invoke(this, EventArgs.Empty); } @@ -145,6 +150,7 @@ namespace TimerApp public void ForceRest() { _restStartTime = DateTime.Now; + _restElapsedSeconds = 0; // 重置休息计数器 ChangeState(MonitorState.Resting); RestStarted?.Invoke(this, EventArgs.Empty); } @@ -158,8 +164,11 @@ namespace TimerApp } else if (CurrentState == MonitorState.Resting) { - TimeSpan timeInRest = DateTime.Now - _restStartTime; - TimeSpan remaining = RestDuration - timeInRest; + // 使用计数器计算剩余时间,保持一致性 + int totalRestSeconds = (int)RestDuration.TotalSeconds; + int remainingSeconds = totalRestSeconds - _restElapsedSeconds; + if (remainingSeconds < 0) remainingSeconds = 0; + TimeSpan remaining = TimeSpan.FromSeconds(remainingSeconds); RestProgressChanged?.Invoke(this, remaining); } }