Skip to content

Commit

Permalink
Convert CheckForUpdates from async void to async Task
Browse files Browse the repository at this point in the history
  • Loading branch information
IhateTrains committed Jan 27, 2025
1 parent c3b3831 commit 3c653fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
19 changes: 6 additions & 13 deletions Fronter.NET/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Fronter;

Expand All @@ -24,7 +24,7 @@ public override void Initialize() {

AvaloniaXamlLoader.Load(this);

LoadTheme();
_ = Task.Run(LoadTheme);
}

public override void OnFrameworkInitializationCompleted() {
Expand All @@ -35,21 +35,14 @@ public override void OnFrameworkInitializationCompleted() {
var mainWindowViewModel = new MainWindowViewModel(window.FindControl<DataGrid>("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;
Expand Down Expand Up @@ -93,7 +86,7 @@ public static void SetTheme(string themeName) {
/// Sets and saves a theme
/// </summary>
/// <param name="themeName" >Name of the theme to set and save.</param>
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);
Expand Down
19 changes: 9 additions & 10 deletions Fronter.NET/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void CopyToTargetGameModDirectory() {
});
copyThread.Start();
}
public void LaunchConverter() {
public async Task LaunchConverter() {

Check warning on line 187 in Fronter.NET/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / test_and_check_coverage

Method is too long (65 lines; maximum allowed: 60) (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0051.md)

Check warning on line 187 in Fronter.NET/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build (self-hosted, linux)

Method is too long (65 lines; maximum allowed: 60) (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0051.md)

Check warning on line 187 in Fronter.NET/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / build (macos-13)

Method is too long (65 lines; maximum allowed: 60) (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0051.md)

Check warning on line 187 in Fronter.NET/ViewModels/MainWindowViewModel.cs

View workflow job for this annotation

GitHub Actions / test (macos-15)

Method is too long (65 lines; maximum allowed: 60) (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0051.md)
ConvertButtonEnabled = false;
ClearLogGrid();

Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -267,7 +266,7 @@ private async void ShowErrorMessageBox() {
}
}

public async void CheckForUpdates() {
public async Task CheckForUpdates() {
if (!Config.UpdateCheckerEnabled) {
return;
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions Fronter.NET/ViewModels/PathPickerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public PathPickerViewModel(Config config) {
RequiredFiles = new ObservableCollection<RequiredFile>(config.RequiredFiles);

// Create reactive commands.
OpenFolderDialogCommand = ReactiveCommand.Create<RequiredFolder>(OpenFolderDialog);
OpenFileDialogCommand = ReactiveCommand.Create<RequiredFile>(OpenFileDialog);
OpenFolderDialogCommand = ReactiveCommand.CreateFromTask<RequiredFolder>(OpenFolderDialog);
OpenFileDialogCommand = ReactiveCommand.CreateFromTask<RequiredFile>(OpenFileDialog);
}

public ObservableCollection<RequiredFolder> RequiredFolders { get; }
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 3c653fa

Please sign in to comment.