Beispiel #1
0
        /// <summary>
        /// Synchronously acquires the provided SemaphoreSlim and returns an IDisposable interface
        /// that, when disposed, will release the underlying semaphore.
        /// </summary>
        /// <param name="semaphore"> The semaphore to acquire and release. </param>
        /// <returns> A task that completes once the semaphore has been acquired. </returns>
        public static IDisposable AutoReleaseWait(
            this SemaphoreSlim semaphore)
        {
            Contract.Requires(semaphore != null);
            var wrapper = new ReleaseableSemaphoreSlimWrapper(semaphore);

            semaphore.Wait();
            return(wrapper);
        }
Beispiel #2
0
        /// <summary>
        /// Asynchronously acquires the provided SemaphoreSlim and returns an IDisposable interface
        /// that, when disposed, will release the underlying semaphore.
        /// </summary>
        /// <param name="semaphore"> The semaphore to acquire and release. </param>
        /// <returns> A task that completes once the semaphore has been acquired. </returns>
        public static async Task <IDisposable> AutoReleaseWaitAsync(
            this SemaphoreSlim semaphore)
        {
            Contract.Requires(semaphore != null);
            var wrapper = new ReleaseableSemaphoreSlimWrapper(semaphore);
            await semaphore.WaitAsync();

            return(wrapper);
        }