简体   繁体   English

处理AppDomain.CurrentDomain.UnhandledException时是否无法显示表单?

[英]Is it impossible to show forms when handling a AppDomain.CurrentDomain.UnhandledException?

Just a quick question. 只是一个简单的问题。

I can get a form to show with other exceptions, but with the type I'm asking about, I get the system "application is no longer working" dialog: 我可以得到一个表单以显示其他异常,但是我问的类型,我得到系统“应用程序不再工作”对话框:

Program.cs: Program.cs中:

        #if !DEBUG
        // Add the event handler for handling UI thread exceptions to the event.
        Application.ThreadException += new ThreadExceptionEventHandler(Logging.Application_ThreadException);

        // Set the unhandled exception mode to force all Windows Forms errors to go through our handler.
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        // Add the event handler for handling non-UI thread exceptions to the event. 
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Logging.CurrentDomain_UnhandledException);
        #endif

        throw new Exception();

Logging.cs: Logging.cs:

    public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        ExceptionHandler((Exception)e.ExceptionObject, true);
    }

    private static void ExceptionHandler(Exception e, bool isFatal)
    {
        LogException(e, isFatal);

        //if (!isFatal)
        //{
            FormException formException = new FormException(isFatal);
            formException.Show();
        //}
        //else // It seems that showing a form when you have an unhandled exception isn't a good idea...
        //{
        //    MessageBox.Show("Crashed",
        //        Program.Name,
        //        MessageBoxButtons.OK,
        //        MessageBoxIcon.Stop);
        //    Program.Exit();
        //}
    }

Maybe this article will be helpful A Simple Class to Catch Unhandled Exceptions in WinForms 也许这篇文章将有助于在WinForms中捕获未处理的异常的简单类

EDIT: I check myself: 编辑:我检查自己:

    static void Main()
    {
        AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
                                                          {
                                                              using (var form = new Form1())
                                                              {
                                                                  form.ShowDialog();
                                                              }
                                                          };

        throw new Exception();
    }

Worked as expected. 按预期工作。 So can you show your code which doesn't work? 那么你能展示一些不起作用的代码吗?


Also from article: 同样来自文章:

   public UnhandledExceptionDlg()
    {
        // Add the event handler for handling UI thread exceptions to the event:
        Application.ThreadException += new ThreadExceptionEventHandler(ThreadExceptionFunction);

        // Set the unhandled exception mode to force all Windows Forms errors to go through our handler:
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

        // Add the event handler for handling non-UI thread exceptions to the event:
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);
    }


    private void UnhandledExceptionFunction(Object sender, UnhandledExceptionEventArgs args)
    {
        // Suppress the Dialog in Debug mode:
        #if !DEBUG
        ShowUnhandledExceptionDlg((Exception)args.ExceptionObject);
        #endif
    }

    private void ShowUnhandledExceptionDlg(Exception e)
    {
        Exception unhandledException = e;

        if(unhandledException == null)
            unhandledException = new Exception("Unknown unhandled Exception was occurred!");

        UnhandledExDlgForm exDlgForm = new UnhandledExDlgForm();
        try
        {
            string appName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
            exDlgForm.Text = appName;
            exDlgForm.labelTitle.Text = String.Format(exDlgForm.labelTitle.Text, appName);
            exDlgForm.checkBoxRestart.Text = String.Format(exDlgForm.checkBoxRestart.Text, appName);
            exDlgForm.checkBoxRestart.Checked = this.RestartApp;

            // Do not show link label if OnShowErrorReport is not handled
            exDlgForm.labelLinkTitle.Visible = (OnShowErrorReport != null);
            exDlgForm.linkLabelData.Visible = (OnShowErrorReport != null);

            // Disable the Button if OnSendExceptionClick event is not handled
            exDlgForm.buttonSend.Enabled = (OnSendExceptionClick != null);

            // Attach reflection to checkbox checked status
            exDlgForm.checkBoxRestart.CheckedChanged += delegate(object o, EventArgs ev)
            {
                this._dorestart = exDlgForm.checkBoxRestart.Checked;
            };

            // Handle clicks on report link label
            exDlgForm.linkLabelData.LinkClicked += delegate(object o, LinkLabelLinkClickedEventArgs ev)
            {
                if(OnShowErrorReport != null)
                {
                    SendExceptionClickEventArgs ar = new SendExceptionClickEventArgs(true, unhandledException, _dorestart);
                    OnShowErrorReport(this, ar);
                }
            };

            // Show the Dialog box:
            bool sendDetails = (exDlgForm.ShowDialog() == System.Windows.Forms.DialogResult.Yes);

            if(OnSendExceptionClick != null)
            {
                SendExceptionClickEventArgs ar = new SendExceptionClickEventArgs(sendDetails, unhandledException, _dorestart);
                OnSendExceptionClick(this, ar);
            }
        }
        finally
        {
            exDlgForm.Dispose();
        }
    }

So it shows form too... 所以它也显示形式...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows服务中处理AppDomain.CurrentDomain.UnhandledException - Handling AppDomain.CurrentDomain.UnhandledException in windows service AppDomain.CurrentDomain.UnhandledException不起作用 - AppDomain.CurrentDomain.UnhandledException Not working 不会调用AppDomain.CurrentDomain.UnhandledException - AppDomain.CurrentDomain.UnhandledException does not get called 处理AppDomain.CurrentDomain.UnhandledException中的异常 - Handle exceptions in AppDomain.CurrentDomain.UnhandledException Revit Addin中的AppDomain.CurrentDomain.UnhandledException - AppDomain.CurrentDomain.UnhandledException in Revit Addin 无法显示来自AppDomain.CurrentDomain.UnhandledException的窗口,该窗口在前台线程中捕获异常-WPF - Unable to show a window from AppDomain.CurrentDomain.UnhandledException that catches an exception in a foreground thread - WPF Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException 都不受尊重 - Neither Application.ThreadException nor AppDomain.CurrentDomain.UnhandledException are respected 我可以在 class 库中向 AppDomain.CurrentDomain.UnhandledException 添加处理程序吗 - Can I Add a handler to AppDomain.CurrentDomain.UnhandledException in a class library 抑制 Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException - Suppressing Application.ThreadException and AppDomain.CurrentDomain.UnhandledException AppDomain.CurrentDomain.UnhandledException不允许我完成功能 - AppDomain.CurrentDomain.UnhandledException does not let me finish my function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM