ARTICLE AD BOX
My AvaloniaUI application starting looks like this:
try { var provider = Services; var logger = provider.GetRequiredService<ILogger>(); logger.Information("Initializing GMenu..."); //await LoadMaterialThemeAsync(provider); await LoadLocalizationAsync(provider).ConfigureAwait(false); logger.Information("Desktop files paths: {paths}", StaticConfiguration.PathToDesktopFiles); logger.Information("Desktop files icons path: {paths}", StaticConfiguration.PathsToRefineIcon); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) desktop.MainWindow = new MainWindow { DataContext = Services.GetRequiredService<MainWindowViewModel>() }; await LoadConfigurationAsync(provider).ConfigureAwait(false); base.OnFrameworkInitializationCompleted(); } catch (Exception e) { Services.GetRequiredService<ILogger>().Error(e, "An error occurred while initializing the application."); Console.WriteLine(e); Environment.Exit(-1); }LoadLocalizationAsync just runs the service that reads .json file with localization (I cannot use .resx because they are not Native AOT compatible), if I remove ConfigureAwait(false, the MainWindow won't be shown, but method will continue executing (because from logs, I see that configuration start loading).
This code was 60% change to fail with exception:
[ERR] [2026-04-28 20:50] [13] An error occured while initializing the application. System.InvalidOperationException: Call from invalid thread at Avalonia.Threading.Dispatcher.<VerifyAccess>g__ThrowVerifyAccess|16_0() at Avalonia.Threading.Dispatcher.VerifyAccess() at Avalonia.Platform.ScreensBase`2.EnsureScreens() at Avalonia.Platform.ScreensBase`2.get_AllScreens() at Avalonia.X11.X11Window..ctor(AvaloniaX11Platform platform, IWindowImpl popupParent, X11WindowMode mode, Boolean overrideRedirect) at Avalonia.X11.X11Window..ctor(AvaloniaX11Platform platform, IWindowImpl popupParent, Boolean overrideRedirect) at Avalonia.X11.AvaloniaX11Platform.CreateWindow() at Avalonia.Controls.Platform.PlatformManager.CreateWindow() at Avalonia.Controls.Window..ctor() at ReactiveUI.Avalonia.ReactiveWindow`1..ctor() in /_/src/ReactiveUI.Avalonia/ReactiveWindow.cs:line 34 at GMenu.Views.MainWindow..ctor() in /home/themakarik/RiderProjects/g-menu/src/GMenu/Views/MainWindow.axaml.cs:line 7 at GMenu.App.OnFrameworkInitializationCompleted() in /home/themakarik/RiderProjects/g-menu/src/GMenu/App.axaml.cs:line 31 System.InvalidOperationException: Call from invalid thread at Avalonia.Threading.Dispatcher.<VerifyAccess>g__ThrowVerifyAccess|16_0() at Avalonia.Threading.Dispatcher.VerifyAccess() at Avalonia.Platform.ScreensBase`2.EnsureScreens() at Avalonia.Platform.ScreensBase`2.get_AllScreens() at Avalonia.X11.X11Window..ctor(AvaloniaX11Platform platform, IWindowImpl popupParent, X11WindowMode mode, Boolean overrideRedirect) at Avalonia.X11.X11Window..ctor(AvaloniaX11Platform platform, IWindowImpl popupParent, Boolean overrideRedirect) at Avalonia.X11.AvaloniaX11Platform.CreateWindow() at Avalonia.Controls.Platform.PlatformManager.CreateWindow() at Avalonia.Controls.Window..ctor() at ReactiveUI.Avalonia.ReactiveWindow`1..ctor() in /_/src/ReactiveUI.Avalonia/ReactiveWindow.cs:line 34 at GMenu.Views.MainWindow..ctor() in /home/themakarik/RiderProjects/g-menu/src/GMenu/Views/MainWindow.axaml.cs:line 7 at GMenu.App.OnFrameworkInitializationCompleted() in /home/themakarik/RiderProjects/g-menu/src/GMenu/App.axaml.cs:line 31It is okay because this method are continue executing in the another thread (not UI Thread), but, if i try to create MainWindow from dispatcher:
var provider = Services; var logger = provider.GetRequiredService<ILogger>(); logger.Information("Initializing GMenu..."); //await LoadMaterialThemeAsync(provider); await LoadLocalizationAsync(provider).ConfigureAwait(false); logger.Information("Desktop files paths: {paths}", StaticConfiguration.PathToDesktopFiles); logger.Information("Desktop files icons path: {paths}", StaticConfiguration.PathsToRefineIcon); Dispatcher.UIThread.Invoke(() => { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) desktop.MainWindow = new MainWindow { DataContext = Services.GetRequiredService<MainWindowViewModel>() }; }); await LoadConfigurationAsync(provider).ConfigureAwait(false); base.OnFrameworkInitializationCompleted();MainWindow again won't show - but more interestingly: my old application loading version, with boilerplate logging works correctly (until I remove ConfigureAwait(false), MainWindow won't show as always):
try { var provider = Services; provider.GetRequiredService<ILogger>().Information("Initializing GMenu..."); //await LoadMaterialThemeAsync(provider); await LoadLocalizationAsync(provider).ConfigureAwait(false); provider.GetRequiredService<ILogger>().Information("Desktop files paths: {paths}", StaticConfiguration.PathToDesktopFiles); provider.GetRequiredService<ILogger>().Information("Desktop files icons path: {paths}", StaticConfiguration.PathsToRefineIcon); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) desktop.MainWindow = new MainWindow { DataContext = Services.GetRequiredService<MainWindowViewModel>() }; await LoadConfigurationAsync(provider).ConfigureAwait(false); base.OnFrameworkInitializationCompleted(); }I don't know how this things are related, can somebody explain me, and how can I fix my code, to make it works again (I can't move configuration loading bottom, because it must load after window showing, or window's content won't be localized).
