Ejemplo n.º 1
0
        public async Task ReleaseAsync()
        {
            if (_lockData == null)
            {
                throw new ApplicationException("You do not own the lock: " + _basePath);
            }

            int newLockCount = _lockData.CountDecrement();

            if (newLockCount > 0)
            {
                return;
            }
            if (newLockCount < 0)
            {
                throw new ApplicationException("Lock count has gone negative for lock: " + _basePath);
            }
            try
            {
                // 释放锁
                await _internals.ReleaseLockAsync(_lockData.LockPath);
            }
            finally
            {
                _lockData = null;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取锁
        /// </summary>
        /// <param name="timeout">超时时间(毫秒)</param>
        /// <returns>成功返回true,超时返回false</returns>
        private async Task <bool> InternalLockAsync(int timeout)
        {
            if (_lockData != null)
            {
                _lockData.CountIncrement();
                return(true);
            }

            // 尝试获取锁
            var lockPath = await _internals.AttemptLockAsync(timeout);

            // 成功获取锁
            if (!string.IsNullOrEmpty(lockPath))
            {
                _lockData = new LockData(lockPath);
                return(true);
            }

            return(false);
        }