liamp47
liamp47

Reputation: 647

C# Method Name Expected - Multithreading

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

Answers (4)

Tilak
Tilak

Reputation: 30698

new Thread(()=> Start(1,2,3,4,5)).Start();

Upvotes: 0

marc wellman
marc wellman

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

Tim S.
Tim S.

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

tmesser
tmesser

Reputation: 7656

You're creating your thread delegate wrong. Try:

Thread thread1 = new Thread(() => Start(1, 2, 3, 4, 5));

Upvotes: 5

Related Questions