0

I'd like to have a method where I can pass another method (without any parameters).
In that method, there would be a thread to call the passed method.

Not really sure how to implement this but the whole idea is something like this:

private static void Main(string[] args)
{
    MethodStarter(Greet)
}

void MethodStarter(Method method)
{
    ThreadStart starter = method;

    _thread = new Thread(starter) { IsBackground = true };
    _thread.Start();
}

void Greet()
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(SendAMessage), "Hello World");            
    Thread.Sleep(5000);

    ThreadPool.QueueUserWorkItem(new WaitCallback(SendAMessage), "How are you today?");         
    Thread.Sleep(5000);
}

void SendAMessage(object arg)
{
    Console.WriteLine(arg as string);
}
yonan2236
  • 13,371
  • 33
  • 95
  • 141

1 Answers1

5

You kind of answered the question in your question, actually. Look at this line:

ThreadStart starter = method;

You are assigning method to a variable of type ThreadStart, so naturally, method should be of type ThreadStart as well:

void MethodStarter(ThreadStart method)
{
    // you don't actually need this line, just pass "method" directly
    // ThreadStart starter = method;

    _thread = new Thread(method) { IsBackground = true };
    _thread.Start();
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313