예제 #1
0
        public static async Task <SemaphoreGuard> Async(SemaphoreSlim semaphore)
        {
            var guard = new SemaphoreGuard(semaphore, false);
            await semaphore.WaitAsync();

            return(guard);
        }
예제 #2
0
        public static async ValueTask <SemaphoreGuard> Async(SemaphoreSlim semaphore)
        {
            if (semaphore == null)
            {
                ThrowArgumnetNullException(nameof(semaphore));
                return(null !);
            }
            var guard = new SemaphoreGuard(semaphore, false);

            // try to grab it sync because it's faster
            if (!semaphore.Wait(0))
            {
                await semaphore.WaitAsync().ConfigureAwait(false);
            }
            return(guard);
        }
예제 #3
0
 private static void ThrowObjectDisposedException(SemaphoreGuard guard) => throw new ObjectDisposedException(guard.ToString());