Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WaitCallBack - ThreadPool

What is the exact purpose of WaitCallback delegate ?

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

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

Can i mean "Wait" in the "TheadPool" until thread is availabe.Once it is available execute the target?

like image 811
user215675 Avatar asked Nov 23 '09 07:11

user215675


2 Answers

The WaitCallback in this case represents a pointer to a function that will be executed on a thread from the thread pool. If no thread is available it will wait until one gets freed.

like image 139
Darin Dimitrov Avatar answered Oct 02 '22 02:10

Darin Dimitrov


From msdn

WaitCallback represents a callback method that you want to execute on a ThreadPool thread. Create the delegate by passing your callback method to the WaitCallback constructor.

Queue your task for execution by passing the WaitCallback delegate to ThreadPool..::.QueueUserWorkItem. Your callback method executes when a thread pool thread becomes available.

System.Threading.WaitCallBack

like image 33
Amal Hashim Avatar answered Oct 02 '22 02:10

Amal Hashim