views:

83

answers:

4

What should I put instead of the "SomeType" in the below function? Delegate seems to be wrong here..

 public static void StartThread(SomeType target)
 {
     ThreadStart tstart = new ThreadStart(target);
     Thread thread = new Thread(tstart);
     thread.Start();
 }

EDIT: I'm not looking for alternative ways to write this.

+3  A: 

You should have the ThreadStart as the argument instead of trying to initialize it within the method.

Matti Virkkunen
I could have but I want to know if it can be done like this. The ThreadStart takes arguments like this: ThreadStart(void () target).. How can I achive this?
radgar
A: 

I think there will be no Sometype, as you are calling some function that will be threaded. Isn't so? Like Thread t = new Thread(new ThreadStart(function_name_here)); t.start();

and void function_name_here() { Blah blah }

FYI there is no return type but VOID.

Anubhav Saini
+2  A: 

Try the System.Action type.

Here my test code:

static void Main(string[] args)
{
    StartThread(() => Console.WriteLine("Hello World!"));

    Console.ReadKey();
}

public static void StartThread(Action target)
{
    ThreadStart tstart = new ThreadStart(target);
    Thread thread = new Thread(tstart);
    thread.Start();
}
PetPaulsen
You're answer isn't wrong, but isn't it more clear to use ThreadStart in this case, since that's what new Thread() takes?
Kirk Woll
I got what you mean. You are right. Actually, that is a better way if I rewrite the method. But the exact answer to my question seems to be System.Action.
radgar
A: 

Replace SomeType with System.Threading.ThreadStart or System.Threading.ParameterizedThreadStart.

Ani