From c3b3831085b471d8a96a6f9b4c703a560f86aa4d Mon Sep 17 00:00:00 2001 From: iht Date: Mon, 27 Jan 2025 00:45:03 +0100 Subject: [PATCH 1/2] Run debug info logging and startup update check in separate threads --- Fronter.NET/App.axaml.cs | 12 ++++++++++-- Fronter.NET/Fronter.csproj | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Fronter.NET/App.axaml.cs b/Fronter.NET/App.axaml.cs index f775c8e3..770b430f 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; namespace Fronter; @@ -34,8 +35,15 @@ 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(); + var debugInfoThread = new Thread(DebugInfo.LogEverything) { + Name = "Debug info logger", + }; + var updateCheckerThread = new Thread(() => mainWindowViewModel.CheckForUpdatesOnStartup()) { + Name = "Update checker", + }; + + desktop.MainWindow.Opened += (sender, args) => debugInfoThread.Start(); + desktop.MainWindow.Opened += (sender, args) => updateCheckerThread.Start(); } base.OnFrameworkInitializationCompleted(); 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 From 3c653fa8afd6b96deac4aed4fce6b6d4aafd086c Mon Sep 17 00:00:00 2001 From: iht Date: Mon, 27 Jan 2025 01:15:11 +0100 Subject: [PATCH 2/2] Convert CheckForUpdates from async void to async Task --- Fronter.NET/App.axaml.cs | 19 ++++++------------- Fronter.NET/ViewModels/MainWindowViewModel.cs | 19 +++++++++---------- Fronter.NET/ViewModels/PathPickerViewModel.cs | 8 ++++---- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/Fronter.NET/App.axaml.cs b/Fronter.NET/App.axaml.cs index 770b430f..fd4f9ebe 100644 --- a/Fronter.NET/App.axaml.cs +++ b/Fronter.NET/App.axaml.cs @@ -10,7 +10,7 @@ using System; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Threading; +using System.Threading.Tasks; namespace Fronter; @@ -24,7 +24,7 @@ public override void Initialize() { AvaloniaXamlLoader.Load(this); - LoadTheme(); + _ = Task.Run(LoadTheme); } public override void OnFrameworkInitializationCompleted() { @@ -35,21 +35,14 @@ public override void OnFrameworkInitializationCompleted() { var mainWindowViewModel = new MainWindowViewModel(window.FindControl("LogGrid")!); window.DataContext = mainWindowViewModel; - var debugInfoThread = new Thread(DebugInfo.LogEverything) { - Name = "Debug info logger", - }; - var updateCheckerThread = new Thread(() => mainWindowViewModel.CheckForUpdatesOnStartup()) { - Name = "Update checker", - }; - - desktop.MainWindow.Opened += (sender, args) => debugInfoThread.Start(); - desktop.MainWindow.Opened += (sender, args) => updateCheckerThread.Start(); + 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; @@ -93,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/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 {