1 year ago

#77557

test-img

Saar

Why it hangs, rather than throw when SemaphoreSlim is disposed while there's a thread waiting on it

Here's a simple implementation:

SemaphoreSlim s = new SemaphoreSlim(0, 1);              // No resource available.
Task<bool> w = s.WaitAsync(TimeSpan.FromSeconds(2));    // Timeout in 2 seconds.
s.Dispose();                                            // Dispose the semaphore earlier than timeout.
await w;                                                // Try await the task. Should it throw or should it hang?

I was expecting it to throw ObjectDisposedException, but it hang on me instead. Could anyone please explain me why?

c#

.net

.net-core

0 Answers

Your Answer

Accepted video resources