C
C#3y ago
Doombox

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();
}
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();
}
9 Replies
RazorSharpFang
When you say Window do you mean that MessageBox ?
Doombox
DoomboxOP3y ago
nope the application window itself, the messagebox closes correctly
RazorSharpFang
You could loop through all the Application Windows and Hide them
Doombox
DoomboxOP3y ago
I tried to grab the window from my IoC container and call .Close() but that didn't seem to work just caused the UI to lockup at a different point
RazorSharpFang
Nono, Hide, not close
Doombox
DoomboxOP3y ago
ah hide works, that's weird, thanks! I guess Close tries to call/access stuff that is unavailable whilst the app is crashing
RazorSharpFang
A closed window cannot be reopened A hidden window can be shown again
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server