From defc82eee3c2faa6cf1730b7591e76c5e18a32c1 Mon Sep 17 00:00:00 2001 From: sincere liu Date: Fri, 10 May 2024 22:23:51 +0800 Subject: [PATCH] If there are many font files, opening the OptionSettingWindow will noticeably lag. The font loading logic can be changed to execute asynchronously. --- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index a6c7f558..165ffa73 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -30,7 +30,7 @@ namespace v2rayN.Views this.Owner = Application.Current.MainWindow; _config = LazyConfig.Instance.GetConfig(); - var lstFonts = GetFonts(Utils.GetFontsPath()); + ViewModel = new OptionSettingViewModel(this); @@ -90,8 +90,7 @@ namespace v2rayN.Views cmbSubConvertUrl.Items.Add(it); }); - lstFonts.ForEach(it => { cmbcurrentFontFamily.Items.Add(it); }); - cmbcurrentFontFamily.Items.Add(string.Empty); + LoadFontsAsync(); //If there are many font files, opening the OptionSettingWindow will noticeably lag. The font loading logic can be changed to execute asynchronously. this.WhenActivated(disposables => { @@ -213,5 +212,34 @@ namespace v2rayN.Views } return lstFonts; } + + private async void LoadFontsAsync() + { + try + { + await Task.Run(() => { + return GetFonts(Utils.GetFontsPath()); + }).ContinueWith(t => { + if (t.Exception == null) + { + Dispatcher.Invoke(() => { + foreach (var font in t.Result) + { + cmbcurrentFontFamily.Items.Add(font); + } + }); + } + else + { + // 处理异常 + Logging.SaveLog("fill fonts error", t.Exception); + } + }); + } + catch (Exception ex) + { + Logging.SaveLog("Async font loading error", ex); + } + } } } \ No newline at end of file