コード例 #1
0
        /// <summary>
        /// Attempt to acquire a <see cref="SemaphoreSlim"/> for a specific key.
        /// If the Semaphore has already been acquired the call will block until the Semaphore is available
        /// </summary>
        /// <param name="key">Key for the Semaphore</param>
        /// <returns>Disposable SemaphoreSlim wrapper</returns>
        public async Task <DisposableSemaphoreSlim <T> > AcquireSemaphoreSlimAsync(T key)
        {
            var semaphore = _semaphoreDictionary.GetOrAdd(key, arg => new SemaphoreSlim(1));
            await semaphore.WaitAsync();

            var disposableSemaphore = new DisposableSemaphoreSlim <T>(key, semaphore, DisposalAction);

            return(disposableSemaphore);
        }
コード例 #2
0
        /// <summary>
        /// Attempt to acquire a <see cref="SemaphoreSlim"/> for a specific key.
        /// If the Semaphore has already been acquired the call will block until the Semaphore is available
        /// </summary>
        /// <param name="key">Key for the Semaphore</param>
        /// <returns>Disposable SemaphoreSlim wrapper</returns>
        public DisposableSemaphoreSlim <T> AcquireSemaphoreSlim(T key)
        {
            var semaphore = _semaphoreDictionary.GetOrAdd(key, arg => new SemaphoreSlim(1));

            semaphore.Wait();

            var disposableSemaphore = new DisposableSemaphoreSlim <T>(key, semaphore, DisposalAction);

            return(disposableSemaphore);
        }