From eb91e8977efab1ded51a55017e31159b6c6be2de Mon Sep 17 00:00:00 2001 From: gokcekantarci <115616017+gokcekantarci@users.noreply.github.com> Date: Wed, 26 Jul 2023 18:48:05 +0300 Subject: [PATCH] [PTRun]Lock mechanism added to GetLocalizedName (#27550) * [PTRun] Lock mechanism added to GetLocalizedName. * [PTRun] Dictionary replaced with ConcurrentDictionary instead of using lock. * [PTRun] Ignoring TryAdd function result. --- .../Wox.Plugin/Common/ShellLocalization.cs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) 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; }