diff --git a/Source/LibationFileManager/FilePathCache.cs b/Source/LibationFileManager/FilePathCache.cs index 96724d4a..469de1fd 100644 --- a/Source/LibationFileManager/FilePathCache.cs +++ b/Source/LibationFileManager/FilePathCache.cs @@ -55,8 +55,9 @@ namespace LibationFileManager { if (!File.Exists(matchingFiles[i].Path)) { + var entryToRemove = matchingFiles[i]; matchingFiles.RemoveAt(i); - cacheChanged |= Remove(matchingFiles[i]); + cacheChanged |= Remove(entryToRemove); } } if (cacheChanged) @@ -79,8 +80,9 @@ namespace LibationFileManager return matchingFiles[i].Path; else { + var entryToRemove = matchingFiles[i]; matchingFiles.RemoveAt(i); - cacheChanged |= Remove(matchingFiles[i]); + cacheChanged |= Remove(entryToRemove); } } return null; @@ -141,6 +143,7 @@ namespace LibationFileManager { [JsonProperty] private readonly ConcurrentDictionary> Dictionary = new(); + private static object lockObject = new(); public List GetIdEntries(string id) { @@ -164,7 +167,21 @@ namespace LibationFileManager } public bool Remove(string id, TEntry entry) - => Dictionary.TryGetValue(id, out List? entries) && entries.Remove(entry); + { + lock (lockObject) + { + if (Dictionary.TryGetValue(id, out List? entries)) + { + var removed = entries?.Remove(entry) ?? false; + if (removed && entries?.Count == 0) + { + Dictionary.Remove(id, out _); + } + return removed; + } + else return false; + } + } } } }