private void MainFrm_Load(object sender, EventArgs e) { try { HideShow(true); listener.Start(); int port = Int32.Parse(db.IniReadValuePivas("SCREEN", "ServerPort").Trim()); tcpServer = new ScreenTcpServer(port); tcpServer.Connected += TcpServer_Connected; tcpServer.Disconnected += TcpServer_Disconnected; tcpServer.ErrorOccurred += TcpServer_ErrorOccurred; tcpServer.EventLogin += TcpServer_EventLogin; tcpServer.EventScreenInfo += TcpServer_EventScreenInfo; tcpServer.Started += TcpServer_Started; tcpServer.Stopped += TcpServer_Stopped; tcpServer.Start(); controller = new ScreenController(tcpServer); controller.ListenStarted += TcpServer_Started; controller.ListenStoped += TcpServer_Stopped; foreach (ColumnHeader ch in listViewDrug.Columns) { ch.Width = -2; } //InitListview(); //ShowTipCenter(); } catch (Exception ex) { InternalLogger.Log.Error("主窗体加载失败" + ex.Message); } }
public event EventHandler ListenStoped; //监听已停止 private void CheckServerStatus() { while (true) { try { Thread.Sleep(60000);//60s=检测周期 if (screenTcpServer != null) { if (screenTcpServer.IsListening) { if (ListenStarted != null && failCount > 0) { ListenStarted(null, null); //从失败中恢复时,触发通知事件 } Interlocked.Add(ref failCount, -failCount); //置零,保证下次不再触发事件 } else { Interlocked.Add(ref failCount, 1);//加1 if (ListenStoped != null && failCount == FAIL_COUNT) { ListenStoped(null, null);//只有等于3时,触发一次 } screenTcpServer.Stop(); Thread.Sleep(100);//稍微暂停一下 screenTcpServer.Start(); } } } catch (Exception ex) { InternalLogger.Log.Error("检测屏服务监听状态出错" + ex.Message); } } }