Mauricio Pastorini
Mauricio Pastorini

Reputation: 852

What's the difference between using ThreadPool with WaitCallback method vs simple method

What's really the difference between option 1 and option 2?

Option 1

WaitCallback callback = new WaitCallback(PrintMessage);
ThreadPool.QueueUserWorkItem(callback, "Hello");

Option 2

ThreadPool.QueueUserWorkItem(PrintMessage, "World");

Simple method:

static void PrintMessage(object obj)
{
   Console.WriteLine(obj);
}

Upvotes: 1

Views: 279

Answers (1)

Vidura Silva
Vidura Silva

Reputation: 64

Note: "WaitCallback" is represents the method to be executed.

Option1: WaitCallback is explicitly called, to queue a method for execution. and this method invocation is on a different thread other than the main thread.

Option2: The .NET framework will wrap the method with WaitCallback. this method invocation is also on a different thread other than the main thread.

Simple Method: this method calling is executed in the main thread itself.

Upvotes: 3

Related Questions