/// <summary>
/// <para>Processes async queued requests that are blocked on needing a free pooled stream
/// works as follows:
/// 1. while there are blocked requests, take one out of the queue
/// 2. Wait for a free connection, when one becomes avail, then notify the request that its there
/// 3. repeat 1 until there are no more queued requests
/// 4. if there are no more requests waiting to for a free stream, then close down this thread
///</para>
/// </summary>
private void AsyncThread()
{
do
{
while (m_QueuedRequests.Count > 0)
{
bool continueLoop = true;
AsyncConnectionPoolRequest asyncState = null;
lock (m_QueuedRequests) {
asyncState = (AsyncConnectionPoolRequest)m_QueuedRequests.Dequeue();
}
WaitHandle [] localWaitHandles = m_WaitHandles;
PooledStream PooledStream = null;
try {
while ((PooledStream == null) && continueLoop)
{
int result = WaitHandle.WaitAny(localWaitHandles, asyncState.CreationTimeout, false);
PooledStream =
Get(asyncState.OwningObject, result, ref continueLoop, ref localWaitHandles);
}
PooledStream.Activate(asyncState.OwningObject, asyncState.AsyncCallback);
} catch (Exception e) {
if (PooledStream != null)
{
PooledStream.Close();
PutConnection(PooledStream, asyncState.OwningObject, asyncState.CreationTimeout);
}
asyncState.AsyncCallback(asyncState.OwningObject, e);
} catch {
if (PooledStream != null)
{
PooledStream.Close();
PutConnection(PooledStream, asyncState.OwningObject, asyncState.CreationTimeout);
}
asyncState.AsyncCallback(asyncState.OwningObject, new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
Thread.Sleep(500);
lock (m_QueuedRequests) {
if (m_QueuedRequests.Count == 0)
{
m_AsyncThread = null;
break;
}
}
} while (true);
}