1

I want to run function with void as return type in new thread, but it always shows this error:

No overload for 'myVoid' matches delegate 'ThreadStart'

and my code :

Thread t = new Thread(new ThreadStart(myVoid)); // <-- Error Shows Here 
t.Start("Test","Test2");

// And The Void :
void myVoid(string text, string text2)
{
    Console.WriteLine(text + text2);
}

How I fix it? What is wrong?

awesoon
  • 32,469
  • 11
  • 74
  • 99
Al00X
  • 1,492
  • 1
  • 12
  • 17
  • Your method needs to be parameter-less. This may help... http://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread – Brian P Dec 28 '15 at 18:17
  • Signature of ThreadStart `public delegate void ThreadStart()` says that it expects a parameterless method. – Marshal Dec 28 '15 at 18:18
  • 2
    Possible duplicate of [C# Threads -ThreadStart Delegate](http://stackoverflow.com/questions/1782210/c-sharp-threads-threadstart-delegate) – Jesse C. Slicer Dec 28 '15 at 18:20

2 Answers2

4

ThreadStart delegate expects a delegate that takes no parameters. If you would like to use myVoid in a thread, you need to provide a way to make a match between myVoid and a no-argument delegate.

One way of doing it is by providing a lambda, like this:

Thread t = new Thread(new ThreadStart(() => myVoid("Test", "Test2")));
t.Start();
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The ThreadStart delegate you are using does not define any arguments.

This means your method myVoid which has 2 string arguments does not match the delegate.

Dietz
  • 578
  • 5
  • 14