private void Btn_DisConn_Click(object sender, EventArgs e) { try { if (IOPCSyncIO20Obj != null) { Marshal.ReleaseComObject(IOPCSyncIO20Obj); IOPCSyncIO20Obj = null; } ServerObj.RemoveGroup(pSvrGroupHandle, 0); if (IOPCGroupStateMgtObj != null) { Marshal.ReleaseComObject(IOPCGroupStateMgtObj); IOPCGroupStateMgtObj = null; } if (MyobjGroup1 != null) { Marshal.ReleaseComObject(MyobjGroup1); MyobjGroup1 = null; } if (ServerObj != null) { Marshal.ReleaseComObject(ServerObj); ServerObj = null; } } catch (System.Exception error) { MessageBox.Show(error.Message, "Result-Stop Server", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 断开连接,调用需要捕捉异常 /// </summary> public void DisConnect() //断开和OPCServer的连接,并释放内存 { try { isConnected = false; isAddGroup = false; isAddItems = false; if (IOPCSyncObj != null) { Marshal.ReleaseComObject(IOPCSyncObj); IOPCSyncObj = null; } ServerObj.RemoveGroup(pSvrGroupHandle, 0); if (GroupObj != null) { Marshal.ReleaseComObject(GroupObj); GroupObj = null; } if (ServerObj != null) { Marshal.ReleaseComObject(ServerObj); ServerObj = null; } } catch (System.Exception error) { MessageBox.Show(error.Message, "断开OPCServer连接出错", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/* 断开与OPC.BCNet.S7连接,由上层应用调用 */ /* 这个函数必须在ConnectOPCServerOfBCNetS7函数执行成功后才能调用 */ public static void DisConnectOPCServerOfBCNetS7() { /* 释放所有OPC资源 */ try { for (int i = 0; i < 1; i++) { Marshal.ReleaseComObject(pIOPCSyncIO[i]); pIOPCSyncIO[i] = null; } Marshal.ReleaseComObject(IOPCAsyncIO2Obj); Serverobj.RemoveGroup(pSvrGroupHandle, 0); for (int i = 0; i < 1; i++) { Marshal.ReleaseComObject(MyobjGroup1); MyobjGroup1 = null; } Marshal.ReleaseComObject(Serverobj); Serverobj = null; connectedOK = false; } catch (Exception ex) /* 断开OPC服务器异常 */ { MessageBox.Show(ex.Message); /* 将异常信息输出到数据库 */ } }