Hi ChrisStopher-0454,
The System.Threading.SemaphoreSlim is a lightweight, fast semaphore that is provided by the CLR and used for waiting within a single process when wait times are expected to be very short.
And calling WaitAsync on the semaphore produces a task that will be completed when that thread has been granted access to the Semaphore.
When the task is ready, release the semaphore. It is vital to always release the semaphore when we are ready, or else we will end up with a Semaphore that is forever locked.
This is why it is important to do the Release within a try...finally clause; program execution may crash or take a different path, this way you are guaranteed execution.
You should note that you can use Dispose() only when all other operations on SemaphoreSlim have completed.
More discusses in these threads you can refer to.
Need to understand the usage of SemaphoreSlim
SemaphoreSlim.WaitAsync before/after try block
Is it necessary to call Dispose on a Semaphore?
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.