Close application without .net framework's error prompt window

前端 未结 2 1864
情书的邮戳
情书的邮戳 2021-01-27 14:12

Codes handles unhandled exceptions as below in my project.

   static void FnUnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs _UnhandledE         


        
相关标签:
2条回答
  • 2021-01-27 14:34

    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
            }
        }
    
    0 讨论(0)
  • 2021-01-27 14:44

    You may want to look into the Application.SetUnhandledExceptionMode method. Calling this with the UnhandledExceptionMode.ThrowException parameter will prevent Winform to route exceptions down to the Application.ThreadException event and thus this dialog won't ever show up.

    You can also alter the app.config file to get the same result:

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

    Personnaly I prefer the hard-coded route.

    Just so we're clear: this will only get rid of the dialog, not solve the actual assembly loading or app freezing problem. :)

    0 讨论(0)
提交回复
热议问题