public bool StartListen() { if (IsStart) { currentRunMode = RunMode.OnListen; return(true); } try { //开始监听plc if (IsConnectPLC && IsConnectSQL)//plc和数据库同时连上 { //开启握手 IsStart = true; //握手信号计时器 Timer_pulse.Start(); //2.开启线程 if (Cts != null) { Cts.Cancel(); //取消FormLoad时PC_PLC_SignalExchange线程 } Cts = new CancellationTokenSource(); switch (InitFormInfo.PLC) { case "Omron": new Task(() => PC_PLC_SignalExchange_Omron(Cts.Token, true)).Start(); break; case "Siemens": new Task(() => PC_PLC_SignalExchange_Siemens(Cts.Token, true)).Start(); break; } //更新开始监听时菜单栏状态 currentRunMode = RunMode.OnListen; } else { //监听失败 IsStart = false; currentRunMode = RunMode.OffListen; MessageBox.Show("PLC或数据库未连接,程序无法启动,请检查连接!"); return(false); } } catch (Exception ex) { UpdateShowMessage(string.Format("程序运行异常,原因:{0}", ex.Message)); LogHelper.Fatal(string.Format("程序运行异常,原因:{0}", ex.Message)); return(false); } return(true); }
public bool StopListen() { if (!IsStart) { currentRunMode = RunMode.OffListen; return(true); } try { //停止监听plc //关闭握手 IsStart = false; //握手信号计时器 Timer_pulse.Stop(); //2.停止交互线程 if (Cts != null) { Cts.Cancel(); //取消FormLoad时PC_PLC_SignalExchange线程 } Cts = new CancellationTokenSource(); switch (InitFormInfo.PLC) { case "Omron": new Task(() => PC_PLC_SignalExchange_Omron(Cts.Token, false)).Start(); break; case "Siemens": new Task(() => PC_PLC_SignalExchange_Siemens(Cts.Token, false)).Start(); break; } //更新停止监听时菜单栏状态 currentRunMode = RunMode.OffListen; } catch (Exception ex) { UpdateShowMessage(string.Format("停止程序异常,原因:{0}", ex.Message)); LogHelper.Fatal(string.Format("停止程序异常,原因:{0}", ex.Message)); currentRunMode = RunMode.OffListen; return(false); } return(true); }