c# - Can't Create New Thread Using Method Call -


i have problem multithreadding. vs2010 don't accept "sendcom(ip, com)". error: expacted method name

    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);     } 

consider expression

new threadstart( sendcom(ip, com) ); 

it says call sendcom , pass result threadstart constructor. that's not want - want threadstart have reference sendcom function, , have new thread pass in ip , com.

the typical way hans passant says:

var t = new thread(new threadstart(() => sendcom(ip, com))); t.start(); 

here you're constructing anonymous function, when invoked, call sendcom. pass new thread. new thread invoke it.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -