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];