/// <summary> /// Removes the value with the specified key. /// </summary> /// <param name="key"> /// The key of the element to remove. /// </param> /// <returns> /// True if the element is successfully found and removed; otherwise, false. /// This method returns false if key is not found. /// </returns> public bool Remove(TKey key) { bool flag; using (WriteLock.Enter(readerWriterLock)) { TValue local; if (items.TryGetValue(key, out local)) { items.Remove(key); DisposeItem(key, local); return(true); } flag = false; } return(flag); }
/// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <param name="key">The key of the value to get or set.</param> /// <returns>The value for the key.</returns> public TValue this[TKey key] { get { TValue local; using (ReadLock.Enter(readerWriterLock)) { local = dictionary[key]; } return(local); } set { using (WriteLock.Enter(readerWriterLock)) { dictionary[key] = value; } } }
/// <summary> /// Adds a key/value pair to the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/> if the key /// does not already exist /// </summary> /// <param name="key">The key of the element to get or add.</param> /// <param name="valueFactory">The function used to generate a value for the key</param> /// <returns>The value for the key. This will be either the existing value for the key if the /// key is already in the dictionary, or the new value for the key as returned by valueFactory /// if the key was not in the dictionary.</returns> public TValue GetOrAdd(TKey key, Func <TKey, TValue> valueFactory) { TValue local2; using (UpgradeableReadLock.Enter(readerWriterLock)) { TValue local; if (dictionary.TryGetValue(key, out local)) { local2 = local; } else { using (WriteLock.Enter(readerWriterLock)) { local = valueFactory(key); dictionary.Add(key, local); local2 = local; } } } return(local2); }
/// <summary> /// Enters a critical write section. Exit the critical section by disposing the return value. /// </summary> /// <returns>A disposable write lock.</returns> /// <exception cref="T:ExitGames.Threading.LockTimeoutException"> /// A write lock could not be obtained within the <see cref="P:ExitGames.Threading.SynchronizedSingletonFactory`2.LockTimeout"/>. /// </exception> protected IDisposable WriterLock() { return(WriteLock.TryEnter(this.readerWriterLockSlim, this.lockTimeout)); }