关闭没有.net框架错误提示窗口的应用程序 [英] Close application without .net framework's error prompt window

查看:24
本文介绍了关闭没有.net框架错误提示窗口的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在我的项目中处理未处理的异常,如下所示.

Codes handles unhandled exceptions as below in my project.

   static void FnUnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs _UnhandledExceptionEventArgs)
        {
            Exception _Exception = (Exception)_UnhandledExceptionEventArgs.ExceptionObject;
            OnUnwantedCloseSendEmail(_Exception.Message);
        }

我正在使用 OnUnwantedCloseSendEmail 方法发送错误报告的电子邮件.OnUnwantedCloseSendEmail 方法的最后一行是 Application.Restart();

I am using OnUnwantedCloseSendEmail method for sending email for error reports. Last line of OnUnwantedCloseSendEmail method is Application.Restart();

当这种方法正确运行时,.net framework 会显示如下错误提示窗口,并且应用程序不会关闭并重新启动,直到按下退出按钮.

When this approach correctly works, .net framework show an prompt window for error as below and application not close and restart until press the quit button.

如何在没有此提示的情况下退出应用程序以及当应用程序也冻结时如何应用此方法.

How can i exit application without this prompt and how can i apply this approach when application frozen too.

推荐答案

你应该能够用这个来捕捉一切

You should be able to catch everything with this

    [STAThread]
    public static void Main()
    {
        // let IDE to handle exceptions
        if (System.Diagnostics.Debugger.IsAttached)
            Run();
        else
            try
            {
                Application.ThreadException += Application_ThreadException;
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                Run();
            }
            catch (Exception e)
            {
                // catch exceptions outside of Application.Run
                UnhandledException(e);
            }
    }

    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        // catch non-ui exceptions
        UnhandledException(e.ExceptionObject as Exception);
    }

    private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        // catch ui exceptions
        UnhandledException(e.Exception);
    }

    private static void UnhandledException(Exception e)
    {
        try
        {
            // here we restart app
        }
        catch
        {
            // if we are here - things are really really bad
        }
    }

这篇关于关闭没有.net框架错误提示窗口的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆