2012-11-08 76 views
3

所以我一直在試圖創建一些代碼,在while循環上發送數據,特別是通過UdpClient向服務器發送數據包。While Loop線程

static void doSend(string ip, int port) 
    { 
     while (isSending) 
     { 
      _sockMain = new UdpClient(ip, port); 
      // Code for datagram here, took it out 
      _sockMain.Send(arr_bData, arr_bData.Length); 
     } 
    } 

但是,當我調用「停止」方法,它卡在一個常量循環,並沒有出來。我怎樣才能把while循環放入一個線程?所以我可以放棄停止線程,取消循環?

+1

如果你從來沒有改變變量'isSending',你將永遠不會從你出去循環 –

+0

是的,當我按一個按鈕isSending = false 但它只是不會退出。 – Daaksin

+0

然後問題可能不在你的循環中,而是在數據報的代碼中。 – AmazingDreams

回答

8

它掛,因爲你的doSend方法適用於UI線程。您可以使用類似下面的類,使其在一個單獨的線程上運行,也可以使用BackgroundWorkerClass

public class DataSender 
    { 
     public DataSender(string ip, int port) 
     { 
      IP = ip; 
      Port = port; 
     } 

     private string IP; 
     private int Port; 
     System.Threading.Thread sender; 
     private bool issending = false; 

     public void StartSending() 
     { 
      if (issending) 
      { 
       // it is already started sending. throw an exception or do something. 
      } 
      issending = true; 
      sender = new System.Threading.Thread(SendData); 
      sender.IsBackground = true; 
      sender.Start(); 
     } 

     public void StopSending() 
     { 
      issending = false; 
      if (sender.Join(200) == false) 
      { 
       sender.Abort(); 
      } 
      sender = null; 
     } 

     private void SendData() 
     { 
      System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP, Port); 
      while (issending) 
      { 
       // Define and assign arr_bData somewhere in class 
       _sockMain.Send(arr_bData, arr_bData.Length); 
      } 
     } 
    } 
+0

我喜歡這個!謝謝塔納! – Daaksin

0

如何使用BREAK聲明?

+0

您可以擴展一下...請... – Daaksin

+0

請檢查鏈接http://msdn.microsoft.com/en-us/library/2aeyhxcd( v = vs.71).aspx,http://www.dotnetperls.com/while。 Break語句可以用來終止while循環。 – TechDo

+0

我只想在點擊一個按鈕時退出它,否則它必須繼續? – Daaksin

1
static bool _isSending; 

static void doSend(string ip, int port) 
{ 
    _isSending = true; 

    while (_isSending) 
    { 
     _sockMain = new UdpClient(ip, port); 
     // ... 
     _sockMain.Send(arr_bData, arr_bData.Length); 
    } 
} 

static void Stop() 
{ 
    // set flag for exiting loop here 
    _isSending = false;  
} 

還要考慮到你的名字在PascalCase方法,即DoSend(甚至StartSending會更好),StopSending

+0

謝謝你,但它掛起?該程序仍然可以獲取數據。 – Daaksin

+0

因爲你確實發送循環。當前發送完成後,下一個不會發生。 –

4

你可以使用backgroundworker線程http://www.dotnetperls.com/backgroundworker 和inside dowork()把你的while循環。 您可以通過使用CancelAsync停止()的代碼,並設置backgroundWorker1.WorkerSupportsCancellation == true

BackgroundWorker bw = new BackgroundWorker(); 
      if (bw.IsBusy != true) 
      { 
       bw.RunWorkerAsync(); 

      } 

      private void bw_DoWork(object sender, DoWorkEventArgs e) 
      { 
       // Run your while loop here and return result. 
       result = // your time consuming function (while loop) 
      } 

      // when you click on some cancel button 
      bw.CancelAsync(); 
+1

這不會以這種方式工作。您需要設置「WorkerSupportsCancellation」,並且您必須在循環中檢查「CancellationPending」。完整的示例可以在[MSDN](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx)上找到。 – Oliver

+0

這可以工作,謝謝Varun。 – Daaksin