Reputation: 647
I've created this program in C# but I'm getting a 'Method Name Expected' error. What am I doing wrong?
for (int i = 0; i < numberofThreads2; i++)
{
Thread thread1 = new Thread(new ThreadStart (Start(1, 2, 3, 4, 5)));
thread1.Start();
}
Upvotes: 1
Views: 1021
Reputation: 5886
ThreadStart
is expecting the name of a method since ThreadStart is a delegate and its purpose is to encapsulate a method.
Like here
public void foo () { }
ThreadStart ts = new ThreadStart(foo);
Upvotes: 1
Reputation: 56536
ThreadStart
is a delegate with the signature void ThreadStart()
. This isn't the same as calling Start(...)
, which actually runs Start
immediately, rather than passing a delegate to do so in the new thread. You're probably looking for Thread thread1 = new Thread(() => Start(1, 2, 3, 4, 5));
, which creates a lambda equiavlent to the following method, which can be converted to a ThreadStart
:
void myLambda()
{
Start(1, 2, 3, 4, 5);
}
Upvotes: 4
Reputation: 7656
You're creating your thread delegate wrong. Try:
Thread thread1 = new Thread(() => Start(1, 2, 3, 4, 5));
Upvotes: 5