From 5edf620b9a38fdffc1b084a013fc511b5692103a Mon Sep 17 00:00:00 2001 From: IhateTrains Date: Mon, 27 Jan 2025 01:17:25 +0100 Subject: [PATCH] Speed up OnFrameworkInitializationCompleted, remove some async void usage (#763) * Run debug info logging and startup update check in separate threads * Convert CheckForUpdates from async void to async Task --- Fronter.NET/App.axaml.cs | 11 ++++++----- Fronter.NET/Fronter.csproj | 2 +- Fronter.NET/ViewModels/MainWindowViewModel.cs | 19 +++++++++---------- Fronter.NET/ViewModels/PathPickerViewModel.cs | 8 ++++---- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Fronter.NET/App.axaml.cs b/Fronter.NET/App.axaml.cs index f775c8e3..fd4f9ebe 100644 --- a/Fronter.NET/App.axaml.cs +++ b/Fronter.NET/App.axaml.cs @@ -10,6 +10,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Threading.Tasks; namespace Fronter; @@ -23,7 +24,7 @@ public override void Initialize() { AvaloniaXamlLoader.Load(this); - LoadTheme(); + _ = Task.Run(LoadTheme); } public override void OnFrameworkInitializationCompleted() { @@ -34,14 +35,14 @@ public override void OnFrameworkInitializationCompleted() { var mainWindowViewModel = new MainWindowViewModel(window.FindControl("LogGrid")!); window.DataContext = mainWindowViewModel; - desktop.MainWindow.Opened += (sender, args) => DebugInfo.LogEverything(); - desktop.MainWindow.Opened += (sender, args) => mainWindowViewModel.CheckForUpdatesOnStartup(); + desktop.MainWindow.Opened += (sender, args) => _ = Task.Run(DebugInfo.LogEverything); + desktop.MainWindow.Opened += (sender, args) => _ = mainWindowViewModel.CheckForUpdatesOnStartup(); } base.OnFrameworkInitializationCompleted(); } - private static async void LoadTheme() { + private static async Task LoadTheme() { if (!File.Exists(FronterThemePath)) { SetTheme(DefaultTheme); return; @@ -85,7 +86,7 @@ public static void SetTheme(string themeName) { /// Sets and saves a theme /// /// Name of the theme to set and save. - public static async void SaveTheme(string themeName) { + public static async Task SaveTheme(string themeName) { SetTheme(themeName); try { await using var fs = new FileStream(FronterThemePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); diff --git a/Fronter.NET/Fronter.csproj b/Fronter.NET/Fronter.csproj index 82cd8525..338dba11 100644 --- a/Fronter.NET/Fronter.csproj +++ b/Fronter.NET/Fronter.csproj @@ -79,7 +79,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Fronter.NET/ViewModels/MainWindowViewModel.cs b/Fronter.NET/ViewModels/MainWindowViewModel.cs index 3ad2ad8c..b2a25aeb 100644 --- a/Fronter.NET/ViewModels/MainWindowViewModel.cs +++ b/Fronter.NET/ViewModels/MainWindowViewModel.cs @@ -184,7 +184,7 @@ private void CopyToTargetGameModDirectory() { }); copyThread.Start(); } - public void LaunchConverter() { + public async Task LaunchConverter() { ConvertButtonEnabled = false; ClearLogGrid(); @@ -201,7 +201,7 @@ public void LaunchConverter() { var converterLauncher = new ConverterLauncher(Config); bool success; - var converterThread = new Thread(() => { + await Task.Run(async () => { ConvertStatus = "CONVERTSTATUSIN"; try { @@ -245,14 +245,13 @@ public void LaunchConverter() { } } else { ConvertStatus = "CONVERTSTATUSPOSTFAIL"; - Dispatcher.UIThread.Post(ShowErrorMessageBox); + await Dispatcher.UIThread.InvokeAsync(ShowErrorMessageBox); ConvertButtonEnabled = true; } }); - converterThread.Start(); } - private async void ShowErrorMessageBox() { + private async Task ShowErrorMessageBox() { var messageBoxWindow = MessageBoxManager .GetMessageBoxStandard(new MessageBoxStandardParams { Icon = Icon.Error, @@ -267,7 +266,7 @@ private async void ShowErrorMessageBox() { } } - public async void CheckForUpdates() { + public async Task CheckForUpdates() { if (!Config.UpdateCheckerEnabled) { return; } @@ -319,11 +318,11 @@ await Dispatcher.UIThread.InvokeAsync(async () => { } } - public void CheckForUpdatesOnStartup() { + public async Task CheckForUpdatesOnStartup() { if (!Config.CheckForUpdatesOnStartup) { return; } - CheckForUpdates(); + await CheckForUpdates(); } #pragma warning disable CA1822 @@ -335,7 +334,7 @@ public void Exit() { } #pragma warning disable CA1822 - public async void OpenAboutDialog() { + public async Task OpenAboutDialog() { #pragma warning restore CA1822 var messageBoxWindow = MessageBoxManager .GetMessageBoxStandard(new MessageBoxStandardParams { @@ -365,7 +364,7 @@ public void SetLanguage(string languageKey) { #pragma warning disable CA1822 public void SetTheme(string themeName) { #pragma warning restore CA1822 - App.SaveTheme(themeName); + _ = App.SaveTheme(themeName); } public string WindowTitle { diff --git a/Fronter.NET/ViewModels/PathPickerViewModel.cs b/Fronter.NET/ViewModels/PathPickerViewModel.cs index aa4f294f..6e57500c 100644 --- a/Fronter.NET/ViewModels/PathPickerViewModel.cs +++ b/Fronter.NET/ViewModels/PathPickerViewModel.cs @@ -23,8 +23,8 @@ public PathPickerViewModel(Config config) { RequiredFiles = new ObservableCollection(config.RequiredFiles); // Create reactive commands. - OpenFolderDialogCommand = ReactiveCommand.Create(OpenFolderDialog); - OpenFileDialogCommand = ReactiveCommand.Create(OpenFileDialog); + OpenFolderDialogCommand = ReactiveCommand.CreateFromTask(OpenFolderDialog); + OpenFileDialogCommand = ReactiveCommand.CreateFromTask(OpenFileDialog); } public ObservableCollection RequiredFolders { get; } @@ -63,7 +63,7 @@ public PathPickerViewModel(Config config) { } [SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Global")] - public async void OpenFolderDialog(RequiredFolder folder) { + public async Task OpenFolderDialog(RequiredFolder folder) { var storageProvider = MainWindow.Instance.StorageProvider; var options = new FolderPickerOpenOptions { @@ -88,7 +88,7 @@ public async void OpenFolderDialog(RequiredFolder folder) { } [SuppressMessage("ReSharper", "MemberCanBeMadeStatic.Global")] - public async void OpenFileDialog(RequiredFile file) { + public async Task OpenFileDialog(RequiredFile file) { var storageProvider = MainWindow.Instance.StorageProvider; var options = new FilePickerOpenOptions {