Beispiel #1
0
        /// <summary>
        /// 获取一个锁(需要自己释放)
        /// </summary>
        /// <param name="key">锁的键</param>
        /// <param name="value">当前占用值</param>
        /// <param name="span">耗时时间</param>
        /// <returns>成功返回true</returns>
        public bool LockTake(string key, string value, TimeSpan span)
        {
            EnsureUtil.NotNullAndNotEmpty(key, "Lockkey");
            EnsureUtil.NotNullAndNotEmpty(value, "Lockvalue");
            var obj = _LockCache.GetValue(key, () => { return(new object()); });

            if (Monitor.TryEnter(obj, span))
            {
                _LockUserCache[key] = value;
                return(true);
            }
            return(false);
        }
Beispiel #2
0
 /// <summary>
 /// 释放一个锁
 /// </summary>
 /// <param name="key">锁的键</param>
 /// <param name="value">当前占用值</param>
 /// <returns>成功返回true</returns>
 public bool LockRelease(string key, string value)
 {
     EnsureUtil.NotNullAndNotEmpty(key, "Lockkey");
     EnsureUtil.NotNullAndNotEmpty(value, "Lockvalue");
     _LockCache.TryGetValue(key, out object obj);
     if (obj != null)
     {
         if (_LockUserCache[key] == value)
         {
             Monitor.Exit(obj);
             return(true);
         }
         return(false);
     }
     return(true);
 }