[DatFile] Fix locking on dictionary

This commit is contained in:
Matt Nadareski
2016-12-01 11:43:09 -08:00
parent 93ad95a218
commit 7dedf77950

View File

@@ -30,7 +30,7 @@ namespace SabreTools.Helper.Dats
private DatFormat _datFormat;
private bool _excludeOf;
private bool _mergeRoms;
private SortedDictionary<string, List<DatItem>> _files;
private SortedDictionary<string, List<DatItem>> _files = new SortedDictionary<string, List<DatItem>>();
private SortedBy _sortedBy;
// Data specific to the Miss DAT type
@@ -274,6 +274,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
@@ -283,6 +285,7 @@ namespace SabreTools.Helper.Dats
// Now return the value
return _files[key];
}
}
set
{
// If the dictionary is null, create it
@@ -291,6 +294,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
@@ -301,14 +306,13 @@ namespace SabreTools.Helper.Dats
_files[key] = value;
}
}
}
/// <summary>
/// Add a new key to the file dictionary
/// </summary>
/// <param name="key">Key in the dictionary to add to</param>
public void Add(string key)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
@@ -316,6 +320,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
@@ -330,8 +336,6 @@ namespace SabreTools.Helper.Dats
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void Add(string key, DatItem value)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
@@ -339,6 +343,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
@@ -356,8 +362,6 @@ namespace SabreTools.Helper.Dats
/// <param name="key">Key in the dictionary to add to</param>
/// <param name="value">Value to add to the dictionary</param>
public void AddRange(string key, List<DatItem> value)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
@@ -365,6 +369,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
// If the key is missing from the dictionary, add it
if (!_files.ContainsKey(key))
{
@@ -382,8 +388,6 @@ namespace SabreTools.Helper.Dats
/// <param name="key">Key in the dictionary to check</param>
/// <returns>True if the key exists, false otherwise</returns>
public bool ContainsKey(string key)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
@@ -391,6 +395,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
return _files.ContainsKey(key);
}
}
@@ -402,8 +408,6 @@ namespace SabreTools.Helper.Dats
public long Count
{
get
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
@@ -411,6 +415,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
return _files.Count;
}
}
@@ -431,8 +437,6 @@ namespace SabreTools.Helper.Dats
public IEnumerable<string> Keys
{
get
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
@@ -440,6 +444,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
return _files.Keys;
}
}
@@ -450,8 +456,6 @@ namespace SabreTools.Helper.Dats
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
lock (_files)
{
// If the dictionary is null, create it
if (_files == null)
@@ -459,6 +463,8 @@ namespace SabreTools.Helper.Dats
_files = new SortedDictionary<string, List<DatItem>>();
}
lock (_files)
{
// If the key is in the dictionary, remove it
if (_files.ContainsKey(key))
{