private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { bool flg = true; while (flg) { this.reader.LoadTagData(ref xtaglist, ref recordcount); for (int i = 0; i < recordcount; i++) { flg = false; string tag = xtaglist[i].Uid + " An:" + xtaglist[i].An; myInvoke mi = new myInvoke(showTags); IAsyncResult aResult; object CallResult; aResult = this.msgLB.BeginInvoke(mi, tag); aResult.AsyncWaitHandle.WaitOne(); CallResult = this.msgLB.EndInvoke(aResult); } if (backgroundWorker1.CancellationPending) { e.Cancel = true; break; } } }
void RecMsg(object sokConnectionparn) { myInvoke myinvoke = new myInvoke(ShowMsg); Socket sokClient = sokConnectionparn as Socket; while (true) { // 定义一个2M的缓存区; byte[] arrMsgRec = new byte[1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = -1; try { length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; } catch (SocketException se) { this.Invoke(myinvoke, "异常:" + se.Message); // 从 通信套接字 集合中删除被中断连接的通信套接字; dict.Remove(sokClient.RemoteEndPoint.ToString()); // 从通信线程集合中删除被中断连接的通信线程对象; dictThread.Remove(sokClient.RemoteEndPoint.ToString()); // 从列表中移除被中断的连接IP -- 委托主线程更新 listBox1.Invoke(new Action(() => { listBox1.Items.Remove(sokClient.RemoteEndPoint.ToString()); })); break; } catch (Exception e) { this.Invoke(myinvoke, "异常:" + e.Message); // 从 通信套接字 集合中删除被中断连接的通信套接字; dict.Remove(sokClient.RemoteEndPoint.ToString()); // 从通信线程集合中删除被中断连接的通信线程对象; dictThread.Remove(sokClient.RemoteEndPoint.ToString()); // 从列表中移除被中断的连接IP listBox1.Invoke(new Action(() => { listBox1.Items.Remove(sokClient.RemoteEndPoint.ToString()); })); break; } if (arrMsgRec[0] == 0) // 表示接收到的是数据; { string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1); // 将接受到的字节数据转化成字符串; if (!strMsg.Contains("-->")) { fileSavePath = fileSavePath + strMsg.Split(':')[1].Replace("\r\n", ""); } this.Invoke(myinvoke, strMsg); } if (arrMsgRec[0] == 1) // 表示接收到的是文件; { using (FileStream fs = new FileStream(fileSavePath, FileMode.Create)) { fs.Write(arrMsgRec, 1, length - 1); this.Invoke(myinvoke, "文件保存成功:" + fileSavePath); } } } }
void RecMsg() { myInvoke myinvoke = new myInvoke(ShowMsg); while (true) { // 定义一个2M的缓存区; byte[] arrMsgRec = new byte[1024 * 1024 * 2]; // 将接受到的数据存入到输入 arrMsgRec中; int length = -1; try { length = sockClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度; } catch (SocketException se) { this.Invoke(myinvoke, "异常;" + se.Message); return; } catch (Exception e) { this.Invoke(myinvoke, "异常;" + e.Message); return; } if (arrMsgRec[0] == 0) // 表示接收到的是消息数据; { string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1); // 将接受到的字节数据转化成字符串; if (!strMsg.Contains("-->")) { fileSavePath = fileSavePath + strMsg.Split(':')[1].Replace("\r\n", ""); } this.Invoke(myinvoke, strMsg); } if (arrMsgRec[0] == 1) // 表示接收到的是文件数据; { try { // 创建文件流,然后根据路径创建文件; using (FileStream fs = new FileStream(fileSavePath, FileMode.Create)) { fs.Write(arrMsgRec, 1, length - 1); this.Invoke(myinvoke, "文件保存成功:" + fileSavePath); } } catch (Exception aaa) { this.Invoke(new Action(() => { MessageBox.Show(aaa.Message); })); } } } }
/// <summary> /// 监听客户端请求的方法; /// </summary> void WatchConnecting() { myInvoke myinvoke = new myInvoke(ShowMsg); while (true) // 持续不断的监听客户端的连接请求; { // 开始监听客户端连接请求,Accept方法会阻断当前的线程; Socket sokConnection = socketWatch.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字; // 想列表控件中添加客户端的IP信息; listBox1.Invoke(new Action(() => { listBox1.Items.Add(sokConnection.RemoteEndPoint.ToString()); })); // 将与客户端连接的 套接字 对象添加到集合中; dict.Add(sokConnection.RemoteEndPoint.ToString(), sokConnection); //ShowMsg("客户端连接成功!"); this.Invoke(myinvoke, "客户端连接成功!"); Thread thr = new Thread(RecMsg); thr.IsBackground = true; thr.Start(sokConnection); dictThread.Add(sokConnection.RemoteEndPoint.ToString(), thr); // 将新建的线程 添加 到线程的集合中去。 } }