diff --git a/MainForm.cs b/MainForm.cs index 0d7fef1..e044255 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -2,6 +2,7 @@ using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; +using Microsoft.Win32; namespace TimerApp { @@ -129,6 +130,8 @@ namespace TimerApp _monitor.Start(); + SystemEvents.PowerModeChanged += OnPowerModeChanged; + // Set tray icon try { @@ -550,6 +553,7 @@ namespace TimerApp } else { + SystemEvents.PowerModeChanged -= OnPowerModeChanged; notifyIcon1.Visible = false; notifyIcon1.Dispose(); } @@ -615,5 +619,15 @@ namespace TimerApp { this.WindowState = FormWindowState.Minimized; } + + private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e) + { + if (e.Mode == PowerModes.Resume) + { + // 系统唤醒时,强制重置媒体播放检测状态, + // 避免因检测线程挂起导致一直误报“正在播放”而无法进入工作状态。 + NativeMethods.InvalidateMediaCache(); + } + } } } diff --git a/NativeMethods.cs b/NativeMethods.cs index 5321aab..0421e3f 100644 --- a/NativeMethods.cs +++ b/NativeMethods.cs @@ -57,6 +57,18 @@ namespace TimerApp return Volatile.Read(ref _mediaPlaying); } + /// + /// 强制失效媒体播放状态缓存 + /// + public static void InvalidateMediaCache() + { + Interlocked.Exchange(ref _mediaLastUpdateMs, -1); + // 不直接重置 _mediaPlaying,以免在检测过程中出现闪烁, + // 只是强制下一次 IsMediaPlaying 触发新的检测。 + // 但如果处于 Resume 状态,假设不播放是安全的。 + Volatile.Write(ref _mediaPlaying, false); + } + private static bool _mediaPlaying; private static long _mediaLastUpdateMs = -1; private static int _mediaUpdateInProgress; @@ -256,7 +268,12 @@ namespace TimerApp thread.IsBackground = true; thread.SetApartmentState(ApartmentState.STA); thread.Start(); - thread.Join(); + + // Wait with timeout (e.g. 2 seconds) to prevent hanging + if (!thread.Join(2000)) + { + return false; + } if (error is not null) {