James King
James King

Reputation: 6343

Semaphore timeout mechanism in C#

Does anyone know how .NET handles a timeout on a call to Semaphore.WaitOne(timeout)?

I'd expect a TimeoutException, but the MSDN documentation doesn't list this in the list of expected exceptions, and I can't seem to find it documented anywhere.

Upvotes: 12

Views: 7290

Answers (1)

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

The method will return false if it times out, and true if it returns a signal:

if (mySemaphore.WaitOne(1000))
{
    // signal received
}
else
{
    // wait timed out
}

Upvotes: 13

Related Questions