0

I have a Problem with 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); } 
4
  • ThreadStart wraps a method to execute, the not result of execution. Commented Apr 4, 2013 at 23:57
  • can you post an exsample how to make it right? Commented Apr 5, 2013 at 0:00
  • 3
    Use new Thread(new ThreadStart(() => sendCom(IP, com))); Commented Apr 5, 2013 at 0:07
  • Note this had nothing to do with windows forms, and almost nothing to do with threading. Commented Apr 5, 2013 at 0:58

1 Answer 1

2

Consider the expression

new ThreadStart( sendCom(IP, com) ); 

It says to call sendCom and pass the result to the ThreadStart constructor. That's not what you want - you want ThreadStart to have a reference to the sendCom function, and have the new thread pass in IP and com.

The typical way to do this is like Hans Passant says:

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

Here you're constructing an anonymous function, that when invoked, will call sendCom. You then pass this to the new Thread. The new thread will invoke it.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, ist works fine, but the thread must be started with thethread.Start();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.