Esempio n. 1
0
        /// <summary>
        ///     usage
        ///     using(var lock = await asyncLock.LockAsync(TimeSpan.FromMilliSeconds(100))) {
        ///     }
        /// </summary>
        /// <param name="timeout">TimeSpan</param>
        /// <returns>disposable</returns>
        public async Task <IDisposable> LockAsync(TimeSpan timeout)
        {
            var cancellationTokenSource = new CancellationTokenSource(timeout);
            await _semaphoreSlim.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(false);

            return(SimpleDisposable.Create(() =>
            {
                cancellationTokenSource.Dispose();
                _semaphoreSlim.Release();
            }));
        }
Esempio n. 2
0
        /// <summary>
        ///     usage
        ///     using(var lock = await asyncLock.LockAsync()) {
        ///     }
        /// </summary>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>disposable</returns>
        public async Task <IDisposable> LockAsync(CancellationToken cancellationToken)
        {
            await _semaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false);

            return(SimpleDisposable.Create(() => { _semaphoreSlim.Release(); }));
        }