private bool ShouldClose()
{
lock (this)
{
while (true)
{
if (!closed)
{
if (!closing)
{
closing = true;
return true;
}
else
{
// Another thread is presently trying to close;
// wait until it finishes one way (closes
// successfully) or another (fails to close)
DoWait();
}
}
else
return false;
}
}
}