精确优化程序关闭问题

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}");
}
}

View File

@@ -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}");
}
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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)