diff --git a/Pilz/Data/Json/JsonDataContainer.cs b/Pilz/Data/Json/JsonDataContainer.cs index f178339..1bd9568 100644 --- a/Pilz/Data/Json/JsonDataContainer.cs +++ b/Pilz/Data/Json/JsonDataContainer.cs @@ -9,21 +9,27 @@ public abstract class JsonDataContainer public virtual HashSet? Set(bool allowCreate) where T : class, IDataObject { - var list = sets.OfType>().FirstOrDefault(); - - if (list == null && allowCreate) + lock (sets) { - list = []; - sets.Add(list); - } + var list = sets.OfType>().FirstOrDefault(); - return list; + if (list == null && allowCreate) + { + list = []; + sets.Add(list); + } + + return list; + } } public virtual void CopyTo(JsonDataContainer container) { - foreach (var set in sets) - container.sets.Add(set); + lock (sets) + { + foreach (var set in sets) + container.sets.Add(set); + } } protected virtual JsonSerializer GetSerializer() diff --git a/Pilz/Data/Json/JsonDataManager.cs b/Pilz/Data/Json/JsonDataManager.cs index d0f95df..7765779 100644 --- a/Pilz/Data/Json/JsonDataManager.cs +++ b/Pilz/Data/Json/JsonDataManager.cs @@ -21,9 +21,7 @@ public class JsonDataManager(JsonDataContainer container) : DataManager protected override void UpdateEntity(T obj) { - var set = container.Set(true); - if (!set.Contains(obj)) - set.Add(obj); + container.Set(true).Add(obj); } protected override void SaveChanges() diff --git a/Pilz/Data/Json/JsonFileContainer.cs b/Pilz/Data/Json/JsonFileContainer.cs index 8417a55..c8b5abf 100644 --- a/Pilz/Data/Json/JsonFileContainer.cs +++ b/Pilz/Data/Json/JsonFileContainer.cs @@ -2,15 +2,23 @@ public class JsonFileContainer(string filePath) : JsonDataContainer { + protected readonly object _lock_FileAction = new(); + public override void Read() { - using var sw = new StreamReader(filePath); - GetSerializer().Populate(sw, this); + lock (_lock_FileAction) + { + using var sw = new StreamReader(filePath); + GetSerializer().Populate(sw, this); + } } public override void Flush() { - using var sw = new StreamWriter(filePath, false); - GetSerializer().Serialize(sw, this); + lock (_lock_FileAction) + { + using var sw = new StreamWriter(filePath, false); + GetSerializer().Serialize(sw, this); + } } } diff --git a/Pilz/Data/Json/JsonStreamContainer.cs b/Pilz/Data/Json/JsonStreamContainer.cs index 359c2e1..ce5876f 100644 --- a/Pilz/Data/Json/JsonStreamContainer.cs +++ b/Pilz/Data/Json/JsonStreamContainer.cs @@ -2,18 +2,25 @@ public class JsonStreamContainer(Stream stream) : JsonDataContainer { + protected readonly object _lock_StreamAction = new(); public override void Read() { - using var sw = new StreamReader(stream); - stream.Position = 0; - GetSerializer().Populate(sw, this); + lock (_lock_StreamAction) + { + using var sw = new StreamReader(stream); + stream.Position = 0; + GetSerializer().Populate(sw, this); + } } public override void Flush() { - using var sw = new StreamWriter(stream); - stream.Position = 0; - GetSerializer().Serialize(sw, this); - stream.SetLength(stream.Position); + lock (_lock_StreamAction) + { + using var sw = new StreamWriter(stream); + stream.Position = 0; + GetSerializer().Serialize(sw, this); + stream.SetLength(stream.Position); + } } } diff --git a/Pilz/Pilz.csproj b/Pilz/Pilz.csproj index 6fce36d..04096b6 100644 --- a/Pilz/Pilz.csproj +++ b/Pilz/Pilz.csproj @@ -5,7 +5,7 @@ latest enable annotations - 2.4.2 + 2.4.3