private void ThreadUIHandleMethod(msgReceiptEventArg eventData) { if (eventData.ErrorHappened) { this.labelUdpstatus.Text = "通讯服务发生错误,已断开"; } else { udpPackage package = new udpPackage(); if (package.Pack(eventData.data)) { _rfidReader.RecvCmd(package, eventData.requestIP, eventData.requestPort); } } }
private void UdprecvEventHandler(object sender, msgReceiptEventArg e) { this.Invoke(new ThreadUIHandle(ThreadUIHandleMethod), e); }
//开始监听 private void recvProc() { while(con) { state = "open"; try { //Receive方法将阻塞进行,直到得到请求信息 //参数ref C_Point将得到请求信息的主机信息 byte[] msg = sv.Receive(ref C_Point); //事件参数 msgReceiptEventArg e = new msgReceiptEventArg(); e.data = new byte[msg.Length]; e.data = msg; //e.data = System.Text.Encoding.UTF8.GetString(msg); e.requestIP = C_Point.Address.ToString(); e.requestPort= C_Point.Port; e.ErrorHappened = false; //如果有事件绑定,就触发事件 if (msgReceiptEvent != null) { msgReceiptEvent(this, e); } } catch(Exception e) { CloseListen(); //msgReceiptEventArg e1 = new msgReceiptEventArg(); //e1.ErrorHappened = true; //e1.errorString = e.Message; //if (msgReceiptEvent != null) //{ // msgReceiptEvent(this, e1); //} } } }