private static void ActionConsumer()
{
while (true)
{
// wait on action pulse
_threadSynch.WaitOne();
ThreadStart action = null;
// try and de-queue an action
lock (lockObject)
{
if (_threadActions.Count > 0)
{
action = _threadActions.Dequeue() as ThreadStart;
}
else
{
// the queue is empty and we are in a critical section
// safely reset the mutex so that everyone waits
// until the next action is queued
_threadSynch.Reset();
}
}
// if we got an action execute it
if (action != null)
{
try
{
action();
}
catch (Exception ex)
{
Debug.Print("Unhandled error in thread pool: " + ex);
}
}
}
}