diff --git a/全自动水压检测仪/Coeffiicientsetting.cs b/全自动水压检测仪/Coeffiicientsetting.cs index c927e47..0db5b96 100644 --- a/全自动水压检测仪/Coeffiicientsetting.cs +++ b/全自动水压检测仪/Coeffiicientsetting.cs @@ -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}"); + } } diff --git a/全自动水压检测仪/DATA/ModbusResourceManager.cs b/全自动水压检测仪/DATA/ModbusResourceManager.cs index 4d410d9..5189c26 100644 --- a/全自动水压检测仪/DATA/ModbusResourceManager.cs +++ b/全自动水压检测仪/DATA/ModbusResourceManager.cs @@ -42,28 +42,64 @@ namespace 全自动水压检测仪.Data public void Dispose() { - try - { - ModbusMaster?.Dispose(); - TcpClient?.Close(); - } - catch - { - // 忽略清理时的异常 - } + ReleaseResource(); } + // 释放资源(统一释放,避免重复关闭) public void ReleaseResource() { - ModbusMaster?.Dispose(); - ModbusMaster = null; + try + { + // 先释放 ModbusMaster + if (ModbusMaster != null) + { + try + { + ModbusMaster.Dispose(); + } + catch (Exception ex) + { + Console.WriteLine($"释放ModbusMaster失败:{ex.Message}"); + } + finally + { + ModbusMaster = null; + } + } - //if (TcpClient?.Connected ?? false) - //{ - // TcpClient.Close(); - //} - TcpClient?.Dispose(); - TcpClient = null; + // 再释放 TcpClient + if (TcpClient != null) + { + try + { + if (TcpClient.Connected) + { + TcpClient.Close(); + } + } + catch (Exception ex) + { + Console.WriteLine($"关闭TcpClient失败:{ex.Message}"); + } + + try + { + TcpClient.Dispose(); + } + catch (Exception ex) + { + Console.WriteLine($"释放TcpClient失败:{ex.Message}"); + } + finally + { + TcpClient = null; + } + } + } + catch (Exception ex) + { + Console.WriteLine($"资源释放异常:{ex.Message}"); + } } } } \ No newline at end of file diff --git a/全自动水压检测仪/NormalTemperatureMode.cs b/全自动水压检测仪/NormalTemperatureMode.cs index f404d5d..4a175d5 100644 --- a/全自动水压检测仪/NormalTemperatureMode.cs +++ b/全自动水压检测仪/NormalTemperatureMode.cs @@ -118,16 +118,19 @@ namespace 全自动水压检测仪 }; timer.Tick += async (s, e) => { - if (!_isCheckingAlarm && _modbusMaster != null) + // 检查窗体和资源状态 + if (this.IsDisposed || !this.IsHandleCreated || _isCheckingAlarm || _modbusMaster == null) { - try - { - await CheckAlarmStatusAsync(); - } - catch (Exception ex) - { - Debug.WriteLine($"报警监控异常: {ex.Message}"); - } + return; + } + + try + { + await CheckAlarmStatusAsync(); + } + catch (Exception ex) + { + Debug.WriteLine($"报警监控异常: {ex.Message}"); } }; return timer; @@ -136,7 +139,13 @@ namespace 全自动水压检测仪 private async Task CheckAlarmStatusAsync() { - if (_isCheckingAlarm || _modbusMaster == null) return; + if (_isCheckingAlarm) return; + + // 检查窗体和Modbus连接状态 + if (this.IsDisposed || !this.IsHandleCreated || _modbusMaster == null) + { + return; + } _isCheckingAlarm = true; @@ -155,12 +164,31 @@ namespace 全自动水压检测仪 foreach (var address in alarmAddresses) { + // 每次循环都检查窗体状态 + if (this.IsDisposed || !this.IsHandleCreated || _modbusMaster == null) + { + break; + } + try { bool[] alarmStatus = await Task.Run(() => { - // 注意:ReadCoils的参数是起始地址和数量 - return _modbusMaster.ReadCoils(1, address, 1); + // 再次检查,避免在后台线程中访问已释放的资源 + if (_modbusMaster == null) + { + return null; + } + + try + { + // 注意:ReadCoils的参数是起始地址和数量 + return _modbusMaster.ReadCoils(1, address, 1); + } + catch + { + return null; + } }); if (alarmStatus != null && alarmStatus.Length > 0) @@ -347,20 +375,16 @@ namespace 全自动水压检测仪 private async Task ReadLeakTestParametersAsync() { - // 检查连接状态 - if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) + // 检查窗体状态 + if (this.IsDisposed || !this.IsHandleCreated) { - SafeInvoke(() => - { - MessageBox.Show("TCP未连接或ModbusMaster未初始化", "提示"); - }); return; } - // 检查UI控件是否可用(避免在窗体释放前调用) - if (!this.IsHandleCreated || this.IsDisposed) + // 检查连接状态 + if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) { - return; + return; // 静默返回,不显示消息框 } try @@ -596,6 +620,12 @@ namespace 全自动水压检测仪 private async Task ReadLeakTestParametersAsyncTwo() { + // 检查窗体状态 + if (this.IsDisposed || !this.IsHandleCreated) + { + return; + } + //是否连接 if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) return; @@ -1164,24 +1194,88 @@ namespace 全自动水压检测仪 private void NormalTemperatureMode_FormClosing(object sender, FormClosingEventArgs e) { - // 停止定时器 - _readTimer?.Stop(); - _readTimer?.Dispose(); - _readTimerTwo?.Stop(); - _readTimerTwo?.Dispose(); - - // 停止报警监控定时器 - _alarmMonitorTimer?.Stop(); - _alarmMonitorTimer?.Dispose(); - - // 释放图表资源 - _chartManager?.Dispose(); - - // 仅用户主动关闭时退出应用 - if (e.CloseReason == CloseReason.UserClosing) + try { - ModbusResourceManager.Instance?.Dispose(); - Application.Exit(); + // 先停止所有定时器,防止在释放资源时继续触发 + if (_readTimer != null) + { + _readTimer.Stop(); + _readTimer.Tick -= null; // 移除事件处理器 + _readTimer.Dispose(); + _readTimer = null; + } + + if (_readTimerTwo != null) + { + _readTimerTwo.Stop(); + _readTimerTwo.Tick -= null; + _readTimerTwo.Dispose(); + _readTimerTwo = null; + } + + // 停止报警监控定时器 + if (_alarmMonitorTimer != null) + { + _alarmMonitorTimer.Stop(); + _alarmMonitorTimer.Tick -= null; + _alarmMonitorTimer.Dispose(); + _alarmMonitorTimer = null; + } + + // 等待异步操作完成 + int waitCount = 0; + while (_isCheckingAlarm && waitCount < 10) + { + System.Threading.Thread.Sleep(100); + waitCount++; + } + + // 释放图表资源 + try + { + _chartManager?.Dispose(); + _chartManager = null; + } + catch (Exception ex) + { + Debug.WriteLine($"释放图表资源失败: {ex.Message}"); + } + + // 释放其他窗体资源 + try + { + _coeffiicientsetting?.Dispose(); + _scanImport?.Dispose(); + _statusSettingsForm?.Dispose(); + _report?.Dispose(); + } + catch (Exception ex) + { + Debug.WriteLine($"释放窗体资源失败: {ex.Message}"); + } + + // 仅用户主动关闭时退出应用 + 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(); + } } } diff --git a/全自动水压检测仪/StatusSettingsForm.cs b/全自动水压检测仪/StatusSettingsForm.cs index 9e438e6..dc1c47a 100644 --- a/全自动水压检测仪/StatusSettingsForm.cs +++ b/全自动水压检测仪/StatusSettingsForm.cs @@ -41,6 +41,12 @@ namespace 全自动水压检测仪 private void UpdateTimer_Tick(object sender, EventArgs e) { + // 检查窗体状态 + if (this.IsDisposed || !this.IsHandleCreated) + { + return; + } + try { UpdateStatusDisplay(); @@ -111,8 +117,20 @@ namespace 全自动水压检测仪 private void StatusSettingsForm_FormClosing(object sender, FormClosingEventArgs e) { - _updateTimer?.Stop(); - _updateTimer?.Dispose(); + try + { + if (_updateTimer != null) + { + _updateTimer.Stop(); + _updateTimer.Tick -= UpdateTimer_Tick; + _updateTimer.Dispose(); + _updateTimer = null; + } + } + catch (Exception ex) + { + Debug.WriteLine($"释放定时器资源失败: {ex.Message}"); + } } private void uiButton_Close_Click(object sender, EventArgs e)