diff --git a/ActivityMonitor.cs b/ActivityMonitor.cs index 4b5ab72..ad9cb74 100644 --- a/ActivityMonitor.cs +++ b/ActivityMonitor.cs @@ -1,5 +1,6 @@ using System; using System.Windows.Forms; +using Microsoft.Win32; namespace TimerApp { @@ -38,6 +39,8 @@ namespace TimerApp _timer = new System.Windows.Forms.Timer(); _timer.Interval = 1000; // 1秒检查一次 _timer.Tick += Timer_Tick; + + SystemEvents.PowerModeChanged += OnPowerModeChanged; } public void Start() @@ -223,9 +226,21 @@ namespace TimerApp if (_disposed) return; _disposed = true; + SystemEvents.PowerModeChanged -= OnPowerModeChanged; + _timer.Stop(); _timer.Tick -= Timer_Tick; _timer.Dispose(); } + + private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) + { + if (e.Mode == PowerModes.Resume) + { + // 系统唤醒时,强制重置媒体播放检测状态, + // 避免因检测线程挂起导致一直误报“正在播放”而无法进入工作状态。 + NativeMethods.InvalidateMediaCache(); + } + } } } diff --git a/MainForm.cs b/MainForm.cs index e044255..9080321 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -130,8 +130,6 @@ namespace TimerApp _monitor.Start(); - SystemEvents.PowerModeChanged += OnPowerModeChanged; - // Set tray icon try { @@ -553,7 +551,6 @@ namespace TimerApp } else { - SystemEvents.PowerModeChanged -= OnPowerModeChanged; notifyIcon1.Visible = false; notifyIcon1.Dispose(); } @@ -599,6 +596,7 @@ namespace TimerApp notifyIcon1.Visible = false; notifyIcon1.Dispose(); Application.Exit(); + Environment.Exit(0); } private void pnlTitle_MouseDown(object sender, MouseEventArgs e) @@ -619,15 +617,5 @@ namespace TimerApp { this.WindowState = FormWindowState.Minimized; } - - private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) - { - if (e.Mode == PowerModes.Resume) - { - // 系统唤醒时,强制重置媒体播放检测状态, - // 避免因检测线程挂起导致一直误报“正在播放”而无法进入工作状态。 - NativeMethods.InvalidateMediaCache(); - } - } } }