2013-04-04 85 views
0

我有一個多線程問題。 VS2010不接受「sendCom(IP,com)」。錯誤:expacted方法名無法使用方法調用創建新線程

private void sendCom(String com) 
    { 

     //send command 
     int i=0; 
     String IP; 
     foreach (var item in checkedListBox1.CheckedItems) 
     { 
      Console.WriteLine(item.ToString()); 
      IP = getIP(item);     
      theThreads[i] = new Thread(new ThreadStart(sendCom(IP, com))); 
      i++; 
     } 
    } 

    private void sendCom(String IP, String com) 
    { 
     theSSHclass.RunSingleCom(IP, com); 
    } 
+0

'ThreadStart'包裝一個方法來執行,執行的不是* result *。 – 2013-04-04 23:57:25

+0

你可以發佈一個exsample如何使它正確嗎? – tux007 2013-04-05 00:00:06

+2

使用'new Thread(new ThreadStart(()=> sendCom(IP,com)));' – 2013-04-05 00:07:39

回答

2

考慮表達

new ThreadStart(sendCom(IP, com)); 

它說叫sendCom,並將結果傳遞到ThreadStart構造。這不是你想要的 - 你想讓ThreadStart引用sendCom函數,並讓新線程通過IPcom

做到這一點的典型方法是像漢斯帕桑特說:

var t = new Thread(new ThreadStart(() => sendCom(IP, com))); 
t.Start(); 

在這裏,我們構建一個匿名函數,調用時,將調用sendCom。然後,您將此傳遞給新線程。新線程將調用它。

+0

謝謝,ist工作正常,但線程必須以thethread.Start()開始。 – tux007 2013-04-08 10:11:49