精确优化程序关闭问题

This commit is contained in:
GukSang.Jin
2026-02-04 18:29:56 +08:00
parent 2ba1f3dae7
commit 17e9cea482
4 changed files with 263 additions and 70 deletions

View File

@@ -50,13 +50,22 @@ namespace 全自动水压检测仪
};
timer.Tick += async (s, e) =>
{
// 检查窗体状态
if (this.IsDisposed || !this.IsHandleCreated)
{
return;
}
if (!_isManualInput && _modbusMaster != null)
{
try
{
await ReadLeakTestParametersAsync();
}
catch { }
catch (Exception ex)
{
Debug.WriteLine($"定时器读取参数异常: {ex.Message}");
}
}
};
@@ -292,24 +301,60 @@ namespace 全自动水压检测仪
private void Coeffiicientsetting_FormClosing(object sender, FormClosingEventArgs e)
{
_cts.Cancel();
_cts.Dispose();
// 停止定时器
_readTimer?.Stop();
_readTimer?.Dispose();
// 仅用户主动关闭时退出应用
if (e.CloseReason == CloseReason.UserClosing)
try
{
ModbusResourceManager.Instance?.Dispose();
Application.Exit();
// 取消异步操作
if (_cts != null && !_cts.IsCancellationRequested)
{
_cts.Cancel();
_cts.Dispose();
_cts = null;
}
// 停止定时器
if (_readTimer != null)
{
_readTimer.Stop();
_readTimer.Tick -= null;
_readTimer.Dispose();
_readTimer = null;
}
// 仅用户主动关闭时退出应用
if (e.CloseReason == CloseReason.UserClosing)
{
try
{
ModbusResourceManager.Instance?.Dispose();
}
catch (Exception ex)
{
Debug.WriteLine($"释放Modbus资源失败: {ex.Message}");
}
Application.Exit();
}
}
catch (Exception ex)
{
Debug.WriteLine($"窗体关闭异常: {ex.Message}");
// 确保应用能够退出
if (e.CloseReason == CloseReason.UserClosing)
{
Application.Exit();
}
}
}
private void Coeffiicientsetting_FormClosed(object sender, FormClosedEventArgs e)
{
_normalTemperatureMode?.Dispose();
try
{
_normalTemperatureMode?.Dispose();
}
catch (Exception ex)
{
Debug.WriteLine($"释放主窗体资源失败: {ex.Message}");
}
}