public void ScheduleIncomingRequest(IEndpointIncomingRequest request)
{
bool tooManyOperations = false;
// queue the request.
lock (m_lock) // i.e. Monitor.Enter(m_lock)
{
// check able to schedule requests.
if (m_stopped || m_queue.Count >= m_maxRequestCount)
{
tooManyOperations = true;
}
else
{
m_queue.Enqueue(request);
// wake up an idle thread to handle the request if there is one
if (m_activeThreadCount < m_totalThreadCount)
{
Monitor.Pulse(m_lock);
}
// start a new thread to handle the request if none are idle and the pool is not full.
else if (m_totalThreadCount < m_maxThreadCount)
{
Thread thread = new Thread(OnProcessRequestQueue);
thread.IsBackground = true;
thread.Start(null);
m_totalThreadCount++;
m_activeThreadCount++; // new threads start in an active state
Utils.Trace("Thread created: " + Thread.CurrentThread.ManagedThreadId + ". Current thread count: " + m_totalThreadCount + ". Active thread count" + m_activeThreadCount);
}
}
}
if (tooManyOperations)
{
request.OperationCompleted(null, StatusCodes.BadTooManyOperations);
}
}
#endregion