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
Post a Comment