Files
zijiuqizonghejianyanyi/Data/BaseWindow.xaml.cs
2026-03-11 15:21:27 +08:00

34 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}