Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        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);
            }
        }