public static ModbusTcpClient GetSingleInstance() { if (singleInstance == null || singleInstance.isDisposed) { singleInstance = new ModbusTcpClient(); } return(singleInstance); }
/// <summary> /// 非密封类可重写的Dispose方法,方便子类继承时可重写 /// 释放资源时区分对待托管资源和非托管资源,而实现自IDisposable的Dispose方法调用时,传入的是true,而终结器调用的时候,传入的是false,当传入true时代表要同时处理托管资源和非托管资源;而传入false则只需要处理非托管资源即可。 /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { if (isDisposed) { return; } //清理托管资源 if (disposing) { } //清理非托管资源 if (socketClient != null) { //释放socket //socketClient.Shutdown(SocketShutdown.Both); socketClient.Close(); socketClient = null; singleInstance = null; } isDisposed = true; }