WaitForIdle() on empty pool?

May 13, 2008 at 4:32 PM
I'm looking at WaitForIdle() code:
        public bool WaitForIdle(int millisecondsTimeout)
        {
            ValidateWaitForIdle();
            return _isIdleWaitHandle.WaitOne(millisecondsTimeout, false);
        }

If called when pool is empty (no waiting, no running tasks), it blocks the calling thread.
I believe it should exit immediately.
Coordinator
May 13, 2008 at 6:18 PM
_isIdleWaitHandle is a ManualResetEvent and not AutoResetEvent.
It is initialized as signaled, so it doesn't block when there are no work items.
_isIdleWaitHandle is set when the work items count in the queue is not zero,
and it is reset once the queue is empty.

Try this, it works!

May 27, 2008 at 5:05 PM
What if the WorkItem calls SmartThreadPool.Shutdown()?
Coordinator
May 28, 2008 at 5:59 AM

During the Shutdown() (I refer to Shutdown() overload) the method aborts all threads in a loop.
The work item will also abort its own thread.

Threads that are aborted are aborted (Thread.Abort())
Threads that are not aborted will complete their work item and then quit. (since the shutdown event is signaled)
If a work item of a thread, that was not aborted, runs forever then the SmartThreadPool will hang!

If you use the SmartThreadPool.Shutdown(false, -1) and there is a non stopping work item then the SmartThreadPool will hang!

I can add detections for these cases, but as a rule of thumb try not to step on your own feet.

Regards,
Ami