2012-10-22 61 views
1
foreach (String host in hostArray) 
{ 
    string s1 = hostArray[t]; 
    string s2 = branchArray[t]; 
    string con_msg; 
    t=t+1; 
    TcpClient socketForServer; 
    try 
    { 
     socketForServer = new TcpClient(s1, 10); 
     con_msg="Connected"; 
    } 
    catch 
    { 
     ListViewItem item = new ListViewItem(); 
     item.BackColor = Color.LightGreen; 
     item.Text = (s2); 
     item.SubItems.Add(s1); 
     item.SubItems.Add("Not found"); 
     listView1.Items.Add(item); 
     con_msg = "Not connected"; 
     // I want to exit this round in here and and start next round how to do it 
    } 

    NetworkStream networkStream = socketForServer.GetStream(); 
    System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); 
    System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream); 
    //code 
} 

我想退出當前輪次並開始下一輪。怎麼做。我不想退出foreach循環,我只想退出本輪並開始下一輪如何停止給定狀態下的foreach循環

+4

聲明:'繼續' – opewix

回答

2
foreach (var item in Items) 
{   
    try 
    { 
    //Exception 
    } 
    catch (Exception) 
    { 
    continue;//Will move to next item of "Items" of FOREACH LOOP 
    } 

    int a = 1 + 1;//If exception gets this line will not execute 
} 
2

continue語句將控制傳遞給出現的封閉迭代語句的下一個迭代。

這裏是如何繼續聲明看起來像在你的代碼:

foreach (String host in hostArray) 
{ 
    string s1 = hostArray[t]; 
    string s2 = branchArray[t]; 
    string con_msg; 
    t=t+1; 
    TcpClient socketForServer; 
    try 
    { 
     socketForServer = new TcpClient(s1, 10); 
     con_msg="Connected"; 
    } 
    catch 
    { 
     ListViewItem item = new ListViewItem(); 
     item.BackColor = Color.LightGreen; 
     item.Text = (s2); 
     item.SubItems.Add(s1); 
     item.SubItems.Add("Not found"); 
     listView1.Items.Add(item); 
     con_msg = "Not connected"; 

     continue; 
    } 

    NetworkStream networkStream = socketForServer.GetStream(); 
    System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); 
    System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream); 
    //code 
} 

轉到聲明程序控制直接傳輸到標籤的語句。

這裏是如何轉到聲明看起來像在你的代碼:

foreach (String host in hostArray) 
{ 
    string s1 = hostArray[t]; 
    string s2 = branchArray[t]; 
    string con_msg; 
    t=t+1; 
    TcpClient socketForServer; 
    try 
    { 
     socketForServer = new TcpClient(s1, 10); 
     con_msg="Connected"; 
    } 
    catch 
    { 
     ListViewItem item = new ListViewItem(); 
     item.BackColor = Color.LightGreen; 
     item.Text = (s2); 
     item.SubItems.Add(s1); 
     item.SubItems.Add("Not found"); 
     listView1.Items.Add(item); 
     con_msg = "Not connected"; 

     goto ErrorOccured; 
    } 

    NetworkStream networkStream = socketForServer.GetStream(); 
    System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); 
    System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream); 
    //code 

    ErrorOccured: 
    Console.WriteLine("Do something else..."); 
} 
+0

您必須在'goto'示例中使用另一個標籤,因爲'//代碼'完成時會執行ErrorOccured。 – Default

+1

我不確定使用「goto」是最佳選擇。也許使用錯誤標誌('bool')會是更好的選擇,然後在最後選擇一個「if(flag)」來選擇正確的代碼路徑。 – Enigmativity

+0

@Default,是的,這就是goto聲明的要點..我只提供了2種可能的解決方案。在他的情況下,「繼續」聲明更加優雅。 – HABJAN

2

使用continue從本輪退出剛剛完成了抓... 通過使用continue語句之前catch,繼續和foreach之間的聲明應該跳過。作爲例子...它就像HABJAN給出的一樣..

foreach (String host in hostArray){ 

     string s1 = hostArray[t]; 
     string s2 = branchArray[t]; 
     string con_msg; 
     t=t+1; 
     TcpClient socketForServer; 

     try{ 
      socketForServer = new TcpClient(s1, 10); 
      con_msg="Connected"; 
     } 
     catch{ 
      ListViewItem item = new ListViewItem(); 
      item.BackColor = Color.LightGreen; 
      item.Text = (s2); 
      item.SubItems.Add(s1); 
      item.SubItems.Add("Not found"); 
      listView1.Items.Add(item); 
      con_msg = "Not connected"; 

      continue; 
     } 

     NetworkStream networkStream = socketForServer.GetStream(); 
     System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream); 
     System.IO.StreamWriter streamWriter =new System.IO.StreamWriter(networkStream); 
     // code 
    }