2015-12-31 44 views
1

我知道有很多關於凍結的foreach形式的東西,但我不能找到一個解決我的問題。我已經有這個程序的服務器部分的工作,我試圖讓一個客戶端,雖然連接到服務器這一代碼將預製txtConn.AppendText("Attempting connection.");的foreach冷凍形式

這是我對套接字連接

private static Socket ConnectSocket(string server, int port, RichTextBox txtConn, BackgroundWorker backgroundWorker1) 
    { 
     Socket s = null; 
     IPHostEntry hostEntry = null; 

     // Get host related information. 
     hostEntry = Dns.GetHostEntry(server); 

     // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid 
     // an exception that occurs when the host IP Address is not compatible with the address family 
     // (typical in the IPv6 case). 
     backgroundWorker1.RunWorkerAsync(); 
     foreach (IPAddress address in hostEntry.AddressList) 
     { 
      IPEndPoint ipe = new IPEndPoint(address, port); 
      Socket tempSocket = 
       new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); 
      Console.WriteLine(ipe); 
      try 
      { 
       attempt++; 
       txtConn.Select(txtConn.TextLength, 0); 
       txtConn.SelectionColor = Color.Aqua; 
       if (attempt == 1) 
       { 
        txtConn.AppendText("Attempting connection."); 
       } 
       else if (attempt > 1) 
       { 
        txtConn.AppendText("\r" + "Attempting connection."); 
       } 
       txtConn.SelectionColor = txtConn.ForeColor; 
       tempSocket.Connect(ipe); 
      } 
      catch (ArgumentNullException ane) 
      { 
       Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); 
       txtConn.Select(txtConn.TextLength, 0); 
       txtConn.SelectionColor = Color.Red; 
       txtConn.AppendText("\r\n" + "Connection could not be established."); 
       txtConn.SelectionColor = txtConn.ForeColor; 
      } 
      catch (SocketException se) 
      { 
       Console.WriteLine("SocketException : {0}", se.ToString()); 
       txtConn.Select(txtConn.TextLength, 0); 
       txtConn.SelectionColor = Color.Red; 
       txtConn.AppendText("\r\n" + "Connection could not be established."); 
       txtConn.SelectionColor = txtConn.ForeColor; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Unexpected exception : {0}", e.ToString()); 
       txtConn.Select(txtConn.TextLength, 0); 
       txtConn.SelectionColor = Color.Red; 
       txtConn.AppendText("\r\n" + "Connection could not be established."); 
       txtConn.SelectionColor = txtConn.ForeColor; 
      } 

      if (tempSocket.Connected) 
      { 
       Console.WriteLine("Connected"); 
       s = tempSocket; 
       break; 
      } 
      else 
      { 
       continue; 
      } 
     } 
     return s; 
    } 
代碼

My program looks like this

當我運行該程序,並與連說了錯誤的端口,它會檢查我的電腦上所有可能的IPS和等待,直到後foreach語句,以顯示錯誤或任何東西。我怎樣才能讓它積極展示這一點? This is when it runs

+0

* foreach凍結表單*?你在說這個程序變得沒有反應嗎?聽起來你需要閱讀'Thread's。 –

+0

我看到你已經有了一個後臺工作者。它執行什麼代碼?爲什麼不把for循環放在後臺工作DoWork事件處理程序中? –

回答

0

您需要在不同的線程運行你的代碼,使用戶界面仍然可以更新,而它正在執行。

做到這一點,最簡單的方法是通過將連接環在線程池一個新的任務。

ThreadPool.QueueUserWorkItem(i => { 
    // Connection loop goes here. 
}); 

如果您需要other options你也可以使用一個任務,BackgroundWorker的,等

0

使用應該使用SocketAsync方法或在另一個線程運行這個東西。您也可以使用BackgroundWorker來做到這一點。

0

我剛纔已經回答了類似的問題here,但在其上展開您的特定情況下,它看起來像你實際上並沒有使用backgroundWorker1。 foreach應該在backgroundWorker1.DoWork事件引用的方法中完成。您還需要爲backgroundWorker1.ProgressChanged事件創建一個方法。您可以使用ReportProgress傳遞一個字符串,然後該消息追加到文本框:

從Worker_DoWork方法的foreach循環,你會報告,而不是直接更新RichTextBox的進展:

worker.ReportProgress(0, "Connection could not be established."); 

然後在Worker_ProgressChanged方法中,您將使用如下所示更新RichTextBox:

txtConn.AppendText(e.UserState.ToString()); 
+0

如何將字符串追加到richtextbox? – MattG5TK

+0

在你的foreach循環,你將調用worker.ReportProgress有消息,而不是直接更新的RichTextBox。所以,在Worker_ProgressChanged方法,你會做這樣的事情txtConn.AppendText(e.UserState.ToString())。我會更新答案以反映這一點。 – Tim

+0

謝謝你的幫助比其他任何東西都更有幫助。謝謝蒂姆。 – MattG5TK