C# 关闭没有.net framework的应用程序';s错误提示窗口

C# 关闭没有.net framework的应用程序';s错误提示窗口,c#,winforms,C#,Winforms,代码在我的项目中处理未处理的异常,如下所示 static void FnUnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs _UnhandledExceptionEventArgs) { Exception _Exception = (Exception)_UnhandledExceptionEventArgs.ExceptionObject;

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

   static void FnUnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs _UnhandledExceptionEventArgs)
        {
            Exception _Exception = (Exception)_UnhandledExceptionEventArgs.ExceptionObject;
            OnUnwantedCloseSendEmail(_Exception.Message);
        }
我正在使用OnUnwantedCloseSendEmail方法发送错误报告的电子邮件。OnUnwantedCloseSendEmail方法的最后一行是Application.Restart()

当此方法正确工作时,.net framework将显示一个错误提示窗口,如下所示,并且在按下退出按钮之前,应用程序不会关闭并重新启动


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

您可能需要研究此方法。使用
UnhandledExceptionMode.ThrowException
参数调用此函数将阻止Winform将异常路由到
应用程序。ThreadException
事件,因此此对话框将永远不会显示

您还可以更改app.config文件以获得相同的结果:

<configuration>
  <system.windows.forms jitDebugging="true"/>
</configuration>

我更喜欢硬编码的路线


我们很清楚:这只会删除对话框,而不会解决实际的程序集加载或应用程序冻结问题。:)

你应该能用这个捕捉到一切

    [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
        }
    }

这是ClickOnce部署的应用程序吗?它支持命令行参数吗?您还可以将自己的事件处理程序添加到Application.ThreadException。如果我没有记错,Winform将在为该事件注册单个处理程序后立即停止显示错误对话框。但坦率地说,我认为上述解决方案效果更好。我建议不要这样做。如果应用程序打算崩溃并重新启动,则无需将异常路由到ThreadException事件。@Crono1981,原因是。。。?您是否需要客户提供的错误日志或OP屏幕截图,以及有史以来信息最丰富的消息—软件无法运行?是否有必要通知最终用户并不重要:Application.ThreadException只能捕获UI线程中的异常,而Domain.UnhandledException可以捕获任何线程中引发的所有异常同一域中的线程。因此,处理这两个事件毫无意义。尤其是如果两者都只调用另一个方法。@Crono1981,那么它就完成了任务,对吗?抓住一切?虽然我现在明白了,谢谢。@Sinatr我应该在哪里添加这种方法?Program.cs?