From 7dedf77950ff79b1a4d8db8491327d019e13332e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 1 Dec 2016 11:43:09 -0800 Subject: [PATCH] [DatFile] Fix locking on dictionary --- SabreTools.Helper/Dats/DatFile.cs | 116 ++++++++++++++++-------------- 1 file changed, 61 insertions(+), 55 deletions(-) diff --git a/SabreTools.Helper/Dats/DatFile.cs b/SabreTools.Helper/Dats/DatFile.cs index d85d7c79..cc92deff 100644 --- a/SabreTools.Helper/Dats/DatFile.cs +++ b/SabreTools.Helper/Dats/DatFile.cs @@ -30,7 +30,7 @@ namespace SabreTools.Helper.Dats private DatFormat _datFormat; private bool _excludeOf; private bool _mergeRoms; - private SortedDictionary> _files; + private SortedDictionary> _files = new SortedDictionary>(); private SortedBy _sortedBy; // Data specific to the Miss DAT type @@ -274,14 +274,17 @@ namespace SabreTools.Helper.Dats _files = new SortedDictionary>(); } - // If the key is missing from the dictionary, add it - if (!_files.ContainsKey(key)) + lock (_files) { - _files.Add(key, new List()); - } + // If the key is missing from the dictionary, add it + if (!_files.ContainsKey(key)) + { + _files.Add(key, new List()); + } - // Now return the value - return _files[key]; + // Now return the value + return _files[key]; + } } set { @@ -291,14 +294,17 @@ namespace SabreTools.Helper.Dats _files = new SortedDictionary>(); } - // If the key is missing from the dictionary, add it - if (!_files.ContainsKey(key)) + lock (_files) { - _files.Add(key, new List()); - } + // If the key is missing from the dictionary, add it + if (!_files.ContainsKey(key)) + { + _files.Add(key, new List()); + } - // Now set the value - _files[key] = value; + // Now set the value + _files[key] = value; + } } } @@ -308,14 +314,14 @@ namespace SabreTools.Helper.Dats /// Key in the dictionary to add to public void Add(string key) { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + lock (_files) { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - // If the key is missing from the dictionary, add it if (!_files.ContainsKey(key)) { @@ -331,14 +337,14 @@ namespace SabreTools.Helper.Dats /// Value to add to the dictionary public void Add(string key, DatItem value) { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + lock (_files) { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - // If the key is missing from the dictionary, add it if (!_files.ContainsKey(key)) { @@ -357,14 +363,14 @@ namespace SabreTools.Helper.Dats /// Value to add to the dictionary public void AddRange(string key, List value) { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + lock (_files) { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - // If the key is missing from the dictionary, add it if (!_files.ContainsKey(key)) { @@ -383,14 +389,14 @@ namespace SabreTools.Helper.Dats /// True if the key exists, false otherwise public bool ContainsKey(string key) { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + lock (_files) { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - return _files.ContainsKey(key); } } @@ -403,14 +409,14 @@ namespace SabreTools.Helper.Dats { get { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + lock (_files) { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - return _files.Count; } } @@ -432,14 +438,14 @@ namespace SabreTools.Helper.Dats { get { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + lock (_files) { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - return _files.Keys; } } @@ -451,14 +457,14 @@ namespace SabreTools.Helper.Dats /// public void Remove(string key) { + // If the dictionary is null, create it + if (_files == null) + { + _files = new SortedDictionary>(); + } + lock (_files) { - // If the dictionary is null, create it - if (_files == null) - { - _files = new SortedDictionary>(); - } - // If the key is in the dictionary, remove it if (_files.ContainsKey(key)) {