From cd695f92ee3e373085ad6bf89b3f719e234bd146 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 30 Mar 2020 21:51:52 +1100 Subject: [PATCH] Pinyin caching- Fix unsupported serialization of ConcurrentDictionary Use Dictionary with lock instead --- Wox.Infrastructure/Alphabet.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Wox.Infrastructure/Alphabet.cs b/Wox.Infrastructure/Alphabet.cs index 487c0a4b0..c67fbbdf1 100644 --- a/Wox.Infrastructure/Alphabet.cs +++ b/Wox.Infrastructure/Alphabet.cs @@ -21,7 +21,7 @@ namespace Wox.Infrastructure { private readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat(); private ConcurrentDictionary PinyinCache; - private BinaryStorage> _pinyinStorage; + private BinaryStorage> _pinyinStorage; private Settings _settings; public void Initialize([NotNull] Settings settings) @@ -36,8 +36,14 @@ namespace Wox.Infrastructure Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () => { - _pinyinStorage = new BinaryStorage>("Pinyin"); - PinyinCache = _pinyinStorage.TryLoad(new ConcurrentDictionary()); + _pinyinStorage = new BinaryStorage>("Pinyin"); + + lock(_pinyinStorage) + { + var loaded = _pinyinStorage.TryLoad(new Dictionary()); + + PinyinCache = new ConcurrentDictionary(loaded); + } // force pinyin library static constructor initialize PinyinHelper.toHanyuPinyinStringArray('T', Format); @@ -79,7 +85,11 @@ namespace Wox.Infrastructure { return; } - _pinyinStorage.Save(PinyinCache); + + lock(_pinyinStorage) + { + _pinyinStorage.Save(PinyinCache.ToDictionary(i => i.Key, i => i.Value)); + } } private static string[] EmptyStringArray = new string[0];