views:

389

answers:

4

I'm writing a class for managing my threading. How do I pass my method that needs threading into my helper class?

All that I'll be doing is creating a new thread and passing the method I've passed through into a new ThreadStart().

Thanks in advance.

+3  A: 

Rather than write your own class to manage threading I suggest you check out the BackgroundWorker class in .NET 2.0 onwards.

Mitch Wheat
Actually, this might be the best way forward, I don't know what I was thinking!
GenericTypeTea
+3  A: 

I'm not sure what you mean by passing void but this should help:

 void StartThread(ThreadStart method) {
     new Thread(method).Start();
 }

and call it with:

 StartThread(myMethod);
Mehrdad Afshari
+1  A: 

You can pass a method or function as a delegate in .NET

Stevo3000
A: 
        public static void Iteration(Happy ending) {
        Thread t = new Thread(new ThreadStart(ending));
        t.Start();
    }

    public delegate void Happy();

inside your main:

Iteration(() => Console.WriteLine("Hello World"));