From cb18ba471f544ef409870f969ec9043f9f3ceeb3 Mon Sep 17 00:00:00 2001 From: Chris Rodgers Date: Wed, 23 May 2018 20:30:39 +0100 Subject: [PATCH] Added locks to ObjectDataCollection --- Effort.Extra/ObjectDataCollection.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Effort.Extra/ObjectDataCollection.cs b/Effort.Extra/ObjectDataCollection.cs index ed389b4..0bf33ab 100644 --- a/Effort.Extra/ObjectDataCollection.cs +++ b/Effort.Extra/ObjectDataCollection.cs @@ -9,6 +9,8 @@ namespace Effort.Extra /// internal class ObjectDataCollection : KeyedCollection { + private readonly object locker = new object(); + /// /// Extracts the key from the specified element. /// @@ -28,9 +30,12 @@ protected override Guid GetKeyForItem(ObjectData item) /// The data. public void AddOrUpdate(ObjectData data) { - if (data == null) throw new ArgumentNullException(nameof(data)); - if (Contains(data.Identifier)) Remove(data.Identifier); - Add(data); + lock (locker) + { + if (data == null) throw new ArgumentNullException(nameof(data)); + if (Contains(data.Identifier)) Remove(data.Identifier); + Add(data); + } } /// @@ -41,8 +46,11 @@ public void AddOrUpdate(ObjectData data) /// true, if the key exists, otherwise false. public bool TryGetValue(Guid key, out ObjectData data) { - data = Contains(key) ? this[key] : null; - return data != null; + lock (locker) + { + data = Contains(key) ? this[key] : null; + return data != null; + } } } } \ No newline at end of file