/// <summary> /// 释放连接 /// </summary> public void Disconnect() { try { if (isConnect == true) { clientHandleDict.Clear(); opcServer.Disconnect(); isConnect = false; } } catch (Exception) { //SCLog.Instance.LogError( "断开OPCServer连接时错误:" + err.Message ); } }
/////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Disconnects the opc client (Removes all items and groups) /// </summary> /////////////////////////////////////////////////////////////////////////////////////////// public void Disconnect() { try { RemoveAllItems(); RemoveAllGroups(); if ((mServer != null)) { mServer.Disconnect(); } } catch (Exception ex) { //Exception Event Console.WriteLine("in Disconnect, error is: " + ex.Message); } finally { mServer = null; GC.Collect(); } }
//################ Sub procedure to initialize OPC Server ###### private void StopOPCClient() { // == // Description : 断开连接OPCServer,返回值False为断开失败,True为断开成功 // Created : 2009/01/19 // Author : zgh // Update : // Updater : // == //bool value = false; try { ObjOPCGroup.IsActive = false; //ObjOPCGroup.Remove(ObjOPCGroup.ServerHandle) ObjOPCGroups.RemoveAll(); ObjOPCItems = null; ObjOPCGroups = null; ObjOPCGroup = null; //ObjOPCGroup.AsyncReadComplete += ObjOPCGroup_AsyncReadComplete; ObjOPCServer.Disconnect(); ObjOPCServer = null; //colTagName = Nothing //value = true; //MessageLog("clsDeviceOPC.StopOPCClient(): Disconnected from OPC device \'" + DeviceName + "\'"); LOG.Info("断开OPC连接 \'" + SourceName + "\'"); } catch (Exception ex) { //value = false; //WarnLog(ex.StackTrace + "\r\n" + ex.Message); // MsgBox(ex.Message) LOG.Error(string.Format("OPCDataSource断开连接出错行:{0},", ex.Message)); } finally { System.GC.Collect(); //强制垃圾回收 } }