Fix errors that collections has been changed while foreach collections

This commit is contained in:
qianlifeng 2014-11-04 18:21:07 +08:00
parent 3c4a9a05a0
commit 5855ff0d27

View File

@ -7,6 +7,7 @@ namespace Wox.Plugin.FindFile.MFTSearch
internal class MFTSearcherCache
{
public Dictionary<string, Dictionary<ulong, USNRecord>> VolumeRecords = new Dictionary<string, Dictionary<ulong, USNRecord>>();
public static object locker = new object();
public MFTSearcherCache() { }
@ -25,10 +26,13 @@ namespace Wox.Plugin.FindFile.MFTSearch
{
EnsureVolumeExistInHashTable(volume);
if (!VolumeRecords[volume].ContainsKey(record.FRN))
{
lock (locker)
{
VolumeRecords[volume].Add(record.FRN, record);
}
}
}
public void EnsureVolumeExistInHashTable(string volume)
{
@ -46,8 +50,11 @@ namespace Wox.Plugin.FindFile.MFTSearch
private bool DeleteRecordHashTableItem(Dictionary<string, Dictionary<ulong, USNRecord>> hashtable, string volume, ulong frn)
{
if (hashtable.ContainsKey(volume) && hashtable[volume].ContainsKey(frn))
{
lock (locker)
{
hashtable[volume].Remove(frn);
}
return true;
}
else
@ -64,8 +71,11 @@ namespace Wox.Plugin.FindFile.MFTSearch
private bool RealUpdateRecord(string volume, Dictionary<string, Dictionary<ulong, USNRecord>> source, USNRecord record)
{
if (source.ContainsKey(volume) && source[volume].ContainsKey(record.FRN))
{
lock (locker)
{
source[volume][record.FRN] = record;
}
return true;
}
else
@ -77,7 +87,8 @@ namespace Wox.Plugin.FindFile.MFTSearch
public List<USNRecord> FindByName(string filename, long maxResult = -1)
{
List<USNRecord> result = new List<USNRecord>();
lock (locker)
{
foreach (Dictionary<ulong, USNRecord> dictionary in VolumeRecords.Values)
{
foreach (var usnRecord in dictionary)
@ -90,7 +101,7 @@ namespace Wox.Plugin.FindFile.MFTSearch
if (maxResult > 0 && result.Count() >= maxResult) break;
}
}
}
return result;
}