TNTzx
TNTzx

Reputation: 527

Avalonia: How to show information to user after fatal error?

Note that I do not have full understanding behind WPF and Avalonia, so please, cut me some slack :(

I want to show some information to the user before exiting the program once an unhandled exception in an Avalonia app is thrown. This is such that I can show to the user some links to report the problem or do something similar.

The underlying issue here is that message boxes (specifically ones from MessageBox.Avalonia) in Avalonia require a window to show, the program needs to somehow wait until the message box is closed, and the program needs to rethrow the exception to exit it.

Note that I don't need to use message boxes, I can simply open a file to show whenever a fatal error occurs, but I doubt this is the best solution out there... I'd still very much prefer a message box.

I tried searching for WPF-related solutions, but all of them use MessageBox.Show() which "doesn't require a window" to show and wait until the user does an input (as far as i and this question is concerned). This is not present in Avalonia.


These are potential solutions I found but have problems:

Global Try-Catch

    [STAThread]
    public static void Main(string[] args) {
        try
        {
            BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
        }
        catch (Exception ex)
        {
            ExceptionDispatchInfo.Capture(ex).Throw();
        }
    }

I gave up on this solution since this would need to take a window somewhere. It has potential though.

AppDomain.CurrentDomain.UnhandledException

public partial class App : Application
{
    public override void Initialize()
    {
        AppDomain.CurrentDomain.UnhandledException += (s, e) =>
        {
            // ???
        }
    }

    // ...
}

This also fails since it'd need to take a window somewhere. Even if I were to use MessageBoxManager.GetMessageBoxStandard(...).Show() which doesn't require a window, it doesn't block the code which would proceed to raising the exception and exiting the program. That means the user doesn't even get to see the window since the program exits right after the window is shown.

Upvotes: 0

Views: 681

Answers (1)

B0lver
B0lver

Reputation: 41

Have you tried to use async version of the method Show()? The solution, that uses MessageBoxManager.GetMessageBoxStandard(...).Show() might have a chance. If it won't work, try wrapping your MessageBox-calling code in a Task like this:

await Task.Run(() => MessageBoxManager.GetMessageBoxStandard(...).Show());

Upvotes: 0

Related Questions