Example #1
0
 /// <summary>
 /// 类内部调用
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void BKWorkNetMSG_DoWork(object sender, DoWorkEventArgs e)
 {
     try//dll里面,win7环境下无法释放端口
     {
         RecivedMSG MSGHandle = (RecivedMSG)e.Argument;
         while (!BKWorkNetMSG.CancellationPending)
         {
             byte[] rsv = udpc.Receive(ref ipedp);
             string msg = Encoding.ASCII.GetString(rsv);
             MSGHandle?.Invoke(msg);
             Application.DoEvents();
         }
         udpc.Close();
     }
     catch (Exception x)
     {
         MessageBox.Show(x.Message);
         if (udpc != null)
         {
             udpc.Close();
         }
     }
 }
Example #2
0
 /// <summary>
 /// 开始接收网络消息,需要一个函数名作为参数传入,用于回调处理msg数据
 /// </summary>
 /// <param name="HandleMSG"></param>
 public void ListenMSG(RecivedMSG HandleMSG)
 {
     BKWorkNetMSG.WorkerSupportsCancellation = true;
     BKWorkNetMSG.DoWork += new DoWorkEventHandler(BKWorkNetMSG_DoWork);
     BKWorkNetMSG.RunWorkerAsync(HandleMSG);
 }