1
0

SynchronizedDictionary.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections.Generic;
  2. namespace Amib.Threading.Internal
  3. {
  4. internal class SynchronizedDictionary<TKey, TValue>
  5. {
  6. private readonly Dictionary<TKey, TValue> _dictionary;
  7. private readonly object _lock;
  8. public SynchronizedDictionary()
  9. {
  10. _lock = new object();
  11. _dictionary = new Dictionary<TKey, TValue>();
  12. }
  13. public int Count
  14. {
  15. get { return _dictionary.Count; }
  16. }
  17. public bool Contains(TKey key)
  18. {
  19. lock (_lock)
  20. {
  21. return _dictionary.ContainsKey(key);
  22. }
  23. }
  24. public void Remove(TKey key)
  25. {
  26. lock (_lock)
  27. {
  28. _dictionary.Remove(key);
  29. }
  30. }
  31. public object SyncRoot
  32. {
  33. get { return _lock; }
  34. }
  35. public TValue this[TKey key]
  36. {
  37. get
  38. {
  39. lock (_lock)
  40. {
  41. return _dictionary[key];
  42. }
  43. }
  44. set
  45. {
  46. lock (_lock)
  47. {
  48. _dictionary[key] = value;
  49. }
  50. }
  51. }
  52. public Dictionary<TKey, TValue>.KeyCollection Keys
  53. {
  54. get
  55. {
  56. lock (_lock)
  57. {
  58. return _dictionary.Keys;
  59. }
  60. }
  61. }
  62. public Dictionary<TKey, TValue>.ValueCollection Values
  63. {
  64. get
  65. {
  66. lock (_lock)
  67. {
  68. return _dictionary.Values;
  69. }
  70. }
  71. }
  72. public void Clear()
  73. {
  74. lock (_lock)
  75. {
  76. _dictionary.Clear();
  77. }
  78. }
  79. }
  80. }