/// <summary> /// Synchronously waits on the semaphore, and returns a disposable that releases the semaphore when disposed, thus treating this semaphore as a "multi-lock". /// </summary> /// <param name="this">The semaphore to lock.</param> /// <param name="cancellationToken">The cancellation token used to cancel the wait.</param> public static IDisposable Lock(this SemaphoreSlim @this, CancellationToken cancellationToken) { @this.Wait(cancellationToken); return(AnonymousDisposable.Create(() => @this.Release())); }
private static async Task <IDisposable> DoLockAsync(SemaphoreSlim @this, CancellationToken cancellationToken) { await @this.WaitAsync(cancellationToken).ConfigureAwait(false); return(AnonymousDisposable.Create(() => @this.Release())); }