开发者

Question about DispatcherUnhandledException

开发者 https://www.devze.com 2023-02-04 10:33 出处:网络
The problem in this code is that when it\'s run and I push \"Yes\" button, This is shown: This means that my application stopped working, I just want it to end.

The problem in this code is that when it's run and I push "Yes" button, This is shown:

Question about DispatcherUnhandledException

This means that my application stopped working, I just want it to end.

private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            string message = "An unhandled exception has ocurred, do you want close the app?";
            MessageBoxRes开发者_Python百科ult currentMB = MessageBox.Show(message, "SomeTitleApp", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);

            if (currentMB == MessageBoxResult.Yes)
            { Application.Current.Shutdown(); }
            else
            { e.Handled = true; }
        }


DispatcherUnhandledException isn't the right one. That is only raised when code on the UI thread bombs. You probably got something going kaboom on a worker thread or before the app even managed to get started.

You have to write an event handler for AppDomain.CurrentDomain.UnhandledException. Calling Environment.Exit() stops the WER dialog from showing. Do make sure to log something, nobody much cares for apps that just suddenly flicks out of existence with some traceable reason why. Log e.ExceptionObject.ToString() to get the exception message and the stack trace recorded. Often good enough to diagnose the problem.


MSDN says: This method can be called only from the thread that created the Application object. Are you sure you are calling shutdown from the proper thread?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号