public static async Task <IDisposable> LockAsync(this SemaphoreSlim semaphore, CancellationToken cancellationToken) { var res = new SemaphoreLock(semaphore); try { await semaphore.WaitAsync(cancellationToken); var res2 = res; res = null; return(res2); } finally { res?.Reset(); } }
public static IDisposable Lock(this SemaphoreSlim semaphore) { var res = new SemaphoreLock(semaphore); try { try { semaphore.Wait(); var res2 = res; res = null; return(res2); } finally { res?.Reset(); } } catch (ObjectDisposedException ex) { throw new OperationCanceledException("semaphore was disposed", ex); } }
public static async Task <IDisposable> LockAsync(this SemaphoreSlim semaphore, CancellationToken cancellationToken) { var res = new SemaphoreLock(semaphore); try { try { await semaphore.WaitAsync(cancellationToken); var res2 = res; res = null; return(res2); } finally { res?.Reset(); } } catch (ObjectDisposedException ex) { throw new OperationCanceledException("semaphore was disposed", ex); } }