private void OnProcessRequestQueue(object state)
{
lock (m_lock) // i.e. Monitor.Enter(m_lock)
{
while (true)
{
// check if the queue is empty.
while (m_queue.Count == 0)
{
m_activeThreadCount--;
// wait for a request. end the thread if no activity.
if (m_stopped || (!Monitor.Wait(m_lock, 30000) && m_totalThreadCount > m_minThreadCount))
{
m_totalThreadCount--;
Utils.Trace("Thread ended: " + Thread.CurrentThread.ManagedThreadId + ". Current thread count: " + m_totalThreadCount + ". Active thread count" + m_activeThreadCount);
return;
}
m_activeThreadCount++;
}
IEndpointIncomingRequest request = m_queue.Dequeue();
Monitor.Exit(m_lock);
try
{
// process the request.
m_server.ProcessRequest(request, state);
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected error processing incoming request.");
}
finally
{
Monitor.Enter(m_lock);
}
}
}
}
#endregion