12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System.Collections.Generic;
- internal class SafeDictionary<TKey, TValue>
- {
- private readonly object _Padlock = new object();
- private readonly Dictionary<TKey, TValue> _Dictionary;
- public TValue this[TKey key]
- {
- get
- {
- lock (_Padlock)
- {
- return _Dictionary[key];
- }
- }
- set
- {
- lock (_Padlock)
- {
- _Dictionary[key] = value;
- }
- }
- }
- public SafeDictionary(int capacity)
- {
- _Dictionary = new Dictionary<TKey, TValue>(capacity);
- }
- public SafeDictionary()
- {
- _Dictionary = new Dictionary<TKey, TValue>();
- }
- public bool TryGetValue(TKey key, out TValue value)
- {
- lock (_Padlock)
- {
- return _Dictionary.TryGetValue(key, out value);
- }
- }
- public void Add(TKey key, TValue value)
- {
- lock (_Padlock)
- {
- if (!_Dictionary.ContainsKey(key))
- {
- _Dictionary.Add(key, value);
- }
- }
- }
- }
|