fix: 修复软件未完全退出问题

This commit is contained in:
2026-01-19 10:08:07 +08:00
parent fe47293da5
commit 0ab770d464
2 changed files with 16 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using Microsoft.Win32;
namespace TimerApp namespace TimerApp
{ {
@@ -38,6 +39,8 @@ namespace TimerApp
_timer = new System.Windows.Forms.Timer(); _timer = new System.Windows.Forms.Timer();
_timer.Interval = 1000; // 1秒检查一次 _timer.Interval = 1000; // 1秒检查一次
_timer.Tick += Timer_Tick; _timer.Tick += Timer_Tick;
SystemEvents.PowerModeChanged += OnPowerModeChanged;
} }
public void Start() public void Start()
@@ -223,9 +226,21 @@ namespace TimerApp
if (_disposed) return; if (_disposed) return;
_disposed = true; _disposed = true;
SystemEvents.PowerModeChanged -= OnPowerModeChanged;
_timer.Stop(); _timer.Stop();
_timer.Tick -= Timer_Tick; _timer.Tick -= Timer_Tick;
_timer.Dispose(); _timer.Dispose();
} }
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Resume)
{
// 系统唤醒时,强制重置媒体播放检测状态,
// 避免因检测线程挂起导致一直误报“正在播放”而无法进入工作状态。
NativeMethods.InvalidateMediaCache();
}
}
} }
} }

View File

@@ -130,8 +130,6 @@ namespace TimerApp
_monitor.Start(); _monitor.Start();
SystemEvents.PowerModeChanged += OnPowerModeChanged;
// Set tray icon // Set tray icon
try try
{ {
@@ -553,7 +551,6 @@ namespace TimerApp
} }
else else
{ {
SystemEvents.PowerModeChanged -= OnPowerModeChanged;
notifyIcon1.Visible = false; notifyIcon1.Visible = false;
notifyIcon1.Dispose(); notifyIcon1.Dispose();
} }
@@ -599,6 +596,7 @@ namespace TimerApp
notifyIcon1.Visible = false; notifyIcon1.Visible = false;
notifyIcon1.Dispose(); notifyIcon1.Dispose();
Application.Exit(); Application.Exit();
Environment.Exit(0);
} }
private void pnlTitle_MouseDown(object sender, MouseEventArgs e) private void pnlTitle_MouseDown(object sender, MouseEventArgs e)
@@ -619,15 +617,5 @@ namespace TimerApp
{ {
this.WindowState = FormWindowState.Minimized; this.WindowState = FormWindowState.Minimized;
} }
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Resume)
{
// 系统唤醒时,强制重置媒体播放检测状态,
// 避免因检测线程挂起导致一直误报“正在播放”而无法进入工作状态。
NativeMethods.InvalidateMediaCache();
}
}
} }
} }