using Modbus.Device; using System; using System.Net.Sockets; using System.Windows; namespace 自救器呼吸器综合检验仪.Data { public partial class BaseWindow : Window { // 共享资源(从管理器获取,不自己创建) protected TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient; protected IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster; protected System.Timers.Timer _readTimer { get; set; } = new System.Timers.Timer(); // 窗口关闭时仅停止定时器,不释放资源(资源由管理器统一释放) protected override void OnClosed(EventArgs e) { base.OnClosed(e); _readTimer?.Stop(); _readTimer?.Dispose(); } // 检查资源是否可用(避免空引用报错) protected bool CheckResourceAvailable() { if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null) { MessageBox.Show("TCP连接已断开,请重新初始化连接!", "提示"); return false; } return true; } } }