//关闭并释放任务,该任务要求初始化所有任务 public static void Close() { Task.Run(() => { try { for (int i = 0; i < MonitorDataBaseModel.IOCommunications.Count; i++) { //此处不用线程,在具体实现中用户采用线程 if (MonitorDataBaseModel.IOCommunications[i].CommunicateDriver != null) { ScadaCommunicateKernel driverDll = (ScadaCommunicateKernel)MonitorDataBaseModel.IOCommunications[i].CommunicateDriver; driverDll.CommunctionClose -= CDriverDll_CommunctionClose; driverDll.CommunctionContinue -= CDriverDll_CommunctionContinue; driverDll.CommunctionPause -= CDriverDll_CommunctionPause; driverDll.CommunctionStart -= CDriverDll_CommunctionStart; driverDll.CommunctionStop -= CDriverDll_CommunctionStop; driverDll.DeviceSended -= CDriverDll_DeviceSended; driverDll.DeviceStatusChanged -= CDriverDll_DeviceStatusChanged; driverDll.Exception -= CDriverDll_Exception; driverDll.OnDataReceived -= CDriverDll_OnDataReceived; driverDll.OnShowFormLog -= CDriverDll_OnShowFormLog; driverDll.StopServer(); } } TaskOperator = TaskOperator.关闭; if (OnMonitorOperator != null) { OnMonitorOperator(TaskOperator); } AddLogToMainLog("停止并并关闭采集服务"); if (ClearMemoryTimer != null) { ClearMemoryTimer.Dispose(); ClearMemoryTimer = null; } if (TcpClient != null) { TcpClient.Stop(); TcpClient.Dispose(); TcpClient = null; } } catch (Exception emx) { ThrowExceptionToMain(emx); } }); }
/// <summary> /// 由于用户在登录前需要TCP与服务器通讯,所以在登录前要创建改服务 /// </summary> /// <returns></returns> public static void CreateTCPServer(string Ip) { if (TcpClient != null) { TcpClient.Stop(); TcpClient.Dispose(); TcpClient = null; } AddLogToMainLog("正在创建TCP连接信息"); TcpClient = new IOMonitorTCPClient(); TcpClient.ClientConfig = new IOConfig(); //TcpClient.OnConnectedServer += TcpClient_OnConnectedServer; TcpClient.OnExceptionHanped += TcpClient_OnExceptionHanped; TcpClient.OnTCPClientLoged += TcpClient_OnTCPClientLoged; TcpClient.OnDisConnectedServer += TcpClient_OnDisConnectedServer; TcpClient.Start(Ip); }