Exemple #1
0
 //后台接收信息线程
 void recieveBW_DoWork(object sender, DoWorkEventArgs e)
 {
     try
     {
         while (udpc.Client != null && !recieveBW.CancellationPending)
         {
             if (udpc.Available > 0)  //如果接收缓冲区有信息才接收,防止接收线程阻塞
             {
                 BUDPGram budpg = new BUDPGram();
                 budpg.data = udpc.Receive(ref budpg.iep);
                 recieveBW.ReportProgress(1, budpg);
             }
             System.Threading.Thread.Sleep(recvInterval);
         }
     }
     catch (Exception EX)
     {
         //MessageBox.Show(EX.Message);
     }
 }
Exemple #2
0
 //后台接收信息线程
 void recieveBW_DoWork(object sender, DoWorkEventArgs e)
 {
     try
     {
         IsReceiving = true;
         while (udpc.Client != null && !recieveBW.CancellationPending)
         {
             try
             {
                 List <BUDPGram> list = new List <BUDPGram>();
                 //DateTime start = DateTime.Now;
                 while (udpc.Available > 0)  //如果接收缓冲区有信息才接收,防止接收线程阻塞
                 {
                     BUDPGram budpg = new BUDPGram();
                     budpg.data = udpc.Receive(ref budpg.iep);
                     list.Add(budpg);
                     recieveBW.ReportProgress(1, budpg);
                 }//这里要用while,接收直到全部收完,而不是收一条,等100ms
                 if (list.Count > 0)
                 {
                     //TimeSpan time = DateTime.Now - start;
                     recieveBW.ReportProgress(2, list);
                     //list.Clear();
                 }
                 System.Threading.Thread.Sleep(recvInterval);
             }
             catch (Exception ex)
             {
                 System.Threading.Thread.Sleep(recvInterval);
             }
         }
     }
     catch (Exception EX)
     {
         IsReceiving = false;
         //MessageBox.Show(EX.Message);
     }
 }