Question

When i implement

WaitCallback waitCB = new WaitCallback(DisplayTime);
for (int i = 1; i <= 5; i++)
{
  ThreadPool.QueueUserWorkItem(waitCB, null);
  Thread.Sleep(2000);
}


    public void DisplayTime(object state)
    {
        Console.WriteLine("Current Time {0} ", DateTime.Now);
    }

( 1 ) Does it mean ,my job is queued into to CLR ?

( 2 ) Will CLR process it after finishing the existing pending item in the queue ?

( 3 ) Will the time to process my item in the queue is not predictable?

Was it helpful?

Solution

( 1 ) Does it mean ,my job is queued into to CLR ?

It is queued to be processed as soon as a thread from the pool is available

( 2 ) Will CLR process it after finishing the existing pending item in the queue ?

There can be several jobs executing at the same time... As soon as one of the pool threads is available, it is used to process the next item from the queue

( 3 ) Will the time to process my item in the queue is not predictable?

No, at least not easily... you would have to know how long the queued jobs take to execute, and keep track of each job to calculate the execution time of a given job. Why would you want to know that anyway ?

OTHER TIPS

Your callback will be executed on a threadpool thread based on whatever algorithm the CLR is using to allocate and start threads from its pool.

In your example the callbacks will be processed close to instantly and in a seemingly deterministic fashion. However that could easily change depending on the state of the threadpool. If your process had used up all the threads in the pool then your callback would not get executed until a thread somewhere else completed and was available to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top