/// <summary> /// 将指定的键和值添加到字典中 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Add(TKey key, TValue value) { bool isExisting = false; rwLock.AcquireReaderLock(lockTimeOut); try { if (!dictionary.ContainsKey(key)) { dictionary.Add(key, value); } else { isExisting = true; } } finally { rwLock.ReleaseWriterLock(); } if (isExisting) { throw new IndexOutOfRangeException(); } }
public bool ContainsKey(string key) { Lock.AcquireReaderLock(1000); try { return(settings.ContainsKey(key)); } finally { Lock.ReleaseReaderLock(); } }
/// <summary> /// Returns a reference to the (directed or indirected) edge that connects to a specific successor vertex. This can either be /// a directed edge starting from this vertex and arriving in the successor or an undirected edge connecting both vertices. This operation is thread-safe. /// </summary> /// <param name="successor">The successor to which this vertex connects</param> /// <returns>The edge between this vertex and the successor in question or null if no such edge exists</returns> public Edge GetEdgeToSuccessor(Vertex successor) { _rwl.AcquireReaderLock(-1); Edge e = null; if (_successorToEdgeMap.ContainsKey(successor.ID)) { e = _successorToEdgeMap[successor.ID]; } _rwl.ReleaseReaderLock(); return(e); }
/// <summary> /// Notifies all registered event handlers of the occurance of an event! /// </summary> /// <param name="e">The event that occured</param> /// <param name="sender">The sender of this event</param> /// <param name="eArgs">The event arguments</param> /// <remarks>Overwrite the EventArgs class to set own arguments</remarks> public void Notify(RoadEvent e, object sender, EventArgs eArgs) { try { m_lock.AcquireReaderLock(TIMEOUT); WeakMulticastDelegate eventDelegate; try { eventDelegate = (WeakMulticastDelegate)m_events[e]; } finally { m_lock.ReleaseReaderLock(); } if (eventDelegate == null) { return; } eventDelegate.InvokeSafe(new object[] { e, sender, eArgs }); } catch (ApplicationException ex) { if (log.IsErrorEnabled) { log.Error("Failed to notify event handler!", ex); } } }
/// <summary> /// Constructor /// </summary> public ReadLock(System.Threading.ReaderWriterLock lk, System.TimeSpan timeout) { _lock = lk; if (null == _lock) { throw new System.ArgumentNullException("Error 2719124605: Null lock given"); } try { // If this thread already holds a write-lock then it is not possible // for other threads to hold read- or write-locks, so do nothing. // Note: this design assumes the scope of the write-lock held is bigger // than this instance. if (false == _lock.IsWriterLockHeld) { _lock.AcquireReaderLock(timeout); this._incrementCount(); //this._printCount(); } } catch (System.ApplicationException e) { System.Console.WriteLine("Error 3866214997: Failed to obtain lock for reading. {0}", e.Message); this._printCount(); throw; } }
/// <summary> /// 查找符合条件的对象,并将它们存入一个list中 /// </summary> /// <param name="adapter">查找条件</param> /// <returns>如果返回的list的长度为0,表示没有找到符合条件的对象</returns> public List <T> Search(int adapter, Hashtable paraset) { List <T> result = null; try { obj_rwl.AcquireReaderLock(System.Threading.Timeout.Infinite); //在对象集合中寻找 int getall;//根据管理策略的不同而选择不同的值,如果该策略缓存了所有的数据,则getall为1,表示不需要再到数据库中查询了。 result = this.obj_dependency.Search(ref this.obj_containers, adapter, paraset, out getall); obj_rwl.ReleaseReaderLock(); if (0 == getall) { obj_rwl.AcquireWriterLock(System.Threading.Timeout.Infinite); result.Clear(); result = SearchDB(adapter, paraset); if (result.Count > 0) { foreach (T t in result) { this.obj_dependency.Insert(t.GetMyGuid(), new CCacheItem_WuQi <K, T>(t.GetMyGuid(), t), ref this.obj_containers); } } obj_rwl.ReleaseWriterLock(); } } catch (System.Exception e) { CExceptionContainer_WuQi.ProcessException(e); throw e; } finally { if (obj_rwl.IsReaderLockHeld) { obj_rwl.ReleaseReaderLock(); } if (obj_rwl.IsWriterLockHeld) { obj_rwl.ReleaseWriterLock(); } } return(result); }
public TResult Read <TResult>(Func <T, TResult> readMethod) { readerWriterLock.AcquireReaderLock(int.MaxValue); try { return(readMethod(data)); } finally { readerWriterLock.ReleaseReaderLock(); } }
public Lock(System.Threading.ReaderWriterLock rwl, LockType lt) { rwlock = rwl; if (type == LockType.ForReading) { rwl.AcquireReaderLock(-1); } else if (type == LockType.ForWriting) { rwl.AcquireWriterLock(-1); } }
public bool IsCodeUsed(long timestamp, String code, object user) { try { rwlock.AcquireReaderLock(lockingTimeout); return(codes.Contains(new UsedCode(timestamp, code, user))); } finally { rwlock.ReleaseReaderLock(); } }
public ToJSON() { try { _rw_lock.AcquireReaderLock(5000); // or whatever you deem an acceptable timeout string s = ""; // Serialize here using Newtonsoft return(s); } finally { _rw_lock.ReleaseReaderLock(); } }
public Lock(System.Threading.ReaderWriterLock rwl, LockType lt) { rwlock = rwl; if (type == LockType.ForReading) { rwl.AcquireReaderLock(-1); } else if (type == LockType.ForWriting) { rwl.AcquireWriterLock(-1); } #if DEBUG callingfunction = new System.Diagnostics.StackTrace(); #endif }
public override void CopyTo(BillType[] array) { rwLock.AcquireReaderLock(timeout); try { collection.CopyTo(array); } finally { rwLock.ReleaseReaderLock(); } }
public override void CopyTo(Property[] array) { _rwLock.AcquireReaderLock(Timeout); try { _collection.CopyTo(array); } finally { _rwLock.ReleaseReaderLock(); } }
public override void CopyTo(StatementPhoneNumber[] array) { rwLock.AcquireReaderLock(timeout); try { collection.CopyTo(array); } finally { rwLock.ReleaseReaderLock(); } }
public override void CopyTo(eServiceCategory[] array) { rwLock.AcquireReaderLock(timeout); try { collection.CopyTo(array); } finally { rwLock.ReleaseReaderLock(); } }
public override void CopyTo(PreviousPayment[] array) { rwLock.AcquireReaderLock(timeout); try { collection.CopyTo(array); } finally { rwLock.ReleaseReaderLock(); } }
/// <summary> /// Recupera a classe dinâmica associada com as propriedades informada. /// </summary> /// <param name="properties"></param> /// <returns></returns> public Type GetDynamicClass(IEnumerable <DynamicProperty> properties) { rwLock.AcquireReaderLock(System.Threading.Timeout.Infinite); try { Signature signature = new Signature(properties); Type type; if (!classes.TryGetValue(signature, out type)) { type = CreateDynamicClass(signature.Properties); classes.Add(signature, type); } return(type); } finally { rwLock.ReleaseReaderLock(); } }
/// <summary> /// 获取读者锁 /// </summary> public void AcquireReaderLock() { m_lock.AcquireReaderLock(-1); }