C#
C#

help

Root Question Message

Doombox
Doombox8/24/2022
WPF Handling crashes more gracefully. [Answered]

I'm currently using this setup which correctly logs and displays feedback correctly, however after the messagebox is closed the application window hangs around for a few seconds completely locked up, calling .Dispose() to clean up the NotifyIcon in the tray more than doubles the time. Is there a way to force the window closed?

private bool _hasProvidedCrashFeedback;

private void CurrentDomainOnFirstChanceException(object sender, FirstChanceExceptionEventArgs e) =>
    HandleException(e.Exception);

protected override void OnUnhandledException(DispatcherUnhandledExceptionEventArgs e) =>
    HandleException(e.Exception);

private void HandleException(Exception e)
{
    _log.Fatal(e, "");
    if (_hasProvidedCrashFeedback) return;
    _hasProvidedCrashFeedback = true;
    Dispose(); // this call doubles the time the window hangs around
    MessageBox.Show("See C:\\Users\\USERNAME\\AppData\\Roaming\\F1Desktop\\Logs for technical information.", 
        "F1 Desktop Has Crashed Unexpectedly");
}

public override void Dispose()
{
    GC.SuppressFinalize(this);
    _icon.Dispose();
    JobManager.Stop();
    base.Dispose();
}
RazorSharpFang
RazorSharpFang8/24/2022
When you say Window do you mean that MessageBox ?
Doombox
Doombox8/24/2022
nope the application window itself, the messagebox closes correctly
RazorSharpFang
RazorSharpFang8/24/2022
You could loop through all the Application Windows and Hide them
Doombox
Doombox8/24/2022
I tried to grab the window from my IoC container and call .Close() but that didn't seem to work
Doombox
Doombox8/24/2022
just caused the UI to lockup at a different point
RazorSharpFang
RazorSharpFang8/24/2022
Nono, Hide, not close
Doombox
Doombox8/24/2022
ah hide works, that's weird, thanks!
Doombox
Doombox8/24/2022
I guess Close tries to call/access stuff that is unavailable whilst the app is crashing
RazorSharpFang
RazorSharpFang8/24/2022
A closed window cannot be reopened
A hidden window can be shown again
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy