Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up OnFrameworkInitializationCompleted, remove some async void usage #763

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Fronter.NET/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;

namespace Fronter;

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

AvaloniaXamlLoader.Load(this);

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

public override void OnFrameworkInitializationCompleted() {
Expand All @@ -34,14 +35,14 @@ public override void OnFrameworkInitializationCompleted() {
var mainWindowViewModel = new MainWindowViewModel(window.FindControl<DataGrid>("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;
Expand Down Expand Up @@ -85,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
2 changes: 1 addition & 1 deletion Fronter.NET/Fronter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.1" />
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
<PackageReference Include="Notification.Avalonia" Version="2.1.0" />
<PackageReference Include="PGCG.commonItems" Version="15.1.4" />
<PackageReference Include="PGCG.commonItems" Version="15.1.5" />
<PackageReference Include="Roslynator.Analyzers" Version="4.12.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
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 @@
});
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 @@

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 @@
}
} 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 @@
}
}

public async void CheckForUpdates() {
public async Task CheckForUpdates() {
if (!Config.UpdateCheckerEnabled) {
return;
}
Expand Down Expand Up @@ -319,11 +318,11 @@
}
}

public void CheckForUpdatesOnStartup() {
public async Task CheckForUpdatesOnStartup() {
if (!Config.CheckForUpdatesOnStartup) {
return;
}
CheckForUpdates();
await CheckForUpdates();
}

#pragma warning disable CA1822
Expand All @@ -335,7 +334,7 @@
}

#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 @@
#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
Loading