diff --git a/src/modules/launcher/Wox.Plugin/Common/ShellLocalization.cs b/src/modules/launcher/Wox.Plugin/Common/ShellLocalization.cs index 2ea86afd02..37706a85d9 100644 --- a/src/modules/launcher/Wox.Plugin/Common/ShellLocalization.cs +++ b/src/modules/launcher/Wox.Plugin/Common/ShellLocalization.cs @@ -2,7 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; +using System.Collections.Concurrent; using System.IO; using Wox.Plugin.Common.Interfaces; using Wox.Plugin.Common.Win32; @@ -15,7 +15,7 @@ namespace Wox.Plugin.Common public class ShellLocalization { // Cache for already localized names. This makes localization of already localized string faster. - private Dictionary _localizationCache = new Dictionary(); + private ConcurrentDictionary _localizationCache = new ConcurrentDictionary(); /// /// Returns the localized name of a shell item. @@ -24,8 +24,10 @@ namespace Wox.Plugin.Common /// The localized name as string or . public string GetLocalizedName(string path) { + string lowerInvariantPath = path.ToLowerInvariant(); + // Checking cache if path is already localized - if (_localizationCache.TryGetValue(path.ToLowerInvariant(), out string value)) + if (_localizationCache.TryGetValue(lowerInvariantPath, out string value)) { return value; } @@ -39,12 +41,7 @@ namespace Wox.Plugin.Common shellItem.GetDisplayName(SIGDN.NORMALDISPLAY, out string filename); - if (!_localizationCache.ContainsKey(path.ToLowerInvariant())) - { - // The if condition is required to not get timing problems when called from an parallel execution. - // Without the check we will get "key exists" exceptions. - _localizationCache.Add(path.ToLowerInvariant(), filename); - } + _ = _localizationCache.TryAdd(lowerInvariantPath, filename); return filename; }