private void FinishHandshakeRead(int newState)
{
lock (this)
{
// Lock is redundant here. Included for clarity.
int lockState = Interlocked.Exchange(ref _lockReadState, newState);
if (lockState != LockPendingRead)
{
return;
}
_lockReadState = LockRead;
object obj = _queuedReadStateRequest;
if (obj == null)
{
// Other thread did not get under the lock yet.
return;
}
_queuedReadStateRequest = null;
if (obj is LazyAsyncResult)
{
((LazyAsyncResult)obj).InvokeCallback();
}
else
{
ThreadPool.QueueUserWorkItem(new WaitCallback(CompleteRequestWaitCallback), obj);
}
}
}