Пример #1
0
 internal TValue GetOrCreate()
 {
     if (isdisposed == true)
     {
         return(null);
     }
     if (value == null)
     {
         var iscreate = false;
         var now      = DateTime.Now;
         try
         {
             lock (valueLock)
             {
                 if (isdisposed == true)
                 {
                     return(null);
                 }
                 if (value == null)
                 {
                     value      = create();
                     createTime = DateTime.Now;
                     Interlocked.Increment(ref ib._usageQuantity);
                     iscreate = true;
                 }
                 else
                 {
                     return(value);
                 }
             }
             if (iscreate)
             {
                 if (value != null)
                 {
                     if (firstValue == null)
                     {
                         firstValue = value;                     //记录首次值
                     }
                     else if (firstValue == value)
                     {
                         IsRegisterError = true;                           //第二次与首次相等,注册姿势错误
                     }
                 }
                 ib.OnNotice(new NoticeEventArgs(NoticeType.AutoCreate, key, null, $"{key} 实例+++创建成功,耗时 {DateTime.Now.Subtract(now).TotalMilliseconds}ms,{ib._usageQuantity}/{ib.Quantity}"));
             }
         }
         catch (Exception ex)
         {
             ib.OnNotice(new NoticeEventArgs(NoticeType.AutoCreate, key, ex, $"{key} 实例+++创建失败:{ex.Message}"));
             throw ex;
         }
     }
     lastActiveTime = DateTime.Now;
     Interlocked.Increment(ref activeCounter);
     return(value);
 }
Пример #2
0
 internal T GetOrCreate()
 {
     if (isdisposed == true) return null;
     if (value == null)
     {
         var iscreate = false;
         var now = DateTime.Now;
         try
         {
             lock (valueLock)
             {
                 if (isdisposed == true) return null;
                 if (value == null)
                 {
                     value = create();
                     createTime = DateTime.Now;
                     Interlocked.Increment(ref ib._usageQuantity);
                     iscreate = true;
                 }
                 else
                 {
                     return value;
                 }
             }
             if (iscreate)
                 ib.OnNotice(new NoticeEventArgs(NoticeType.AutoCreate, key, null, $"{key} 实例+++创建成功,耗时 {DateTime.Now.Subtract(now).TotalMilliseconds}ms,{ib._usageQuantity}/{ib.Quantity}"));
         }
         catch (Exception ex)
         {
             ib.OnNotice(new NoticeEventArgs(NoticeType.AutoCreate, key, ex, $"{key} 实例+++创建失败:{ex.Message}"));
             throw ex;
         }
     }
     lastActiveTime = DateTime.Now;
     Interlocked.Increment(ref activeCounter);
     return value;
 }