C # WinForm program quit unexpectedly captured, proceed with the automatic restart

This article reference information online search, and make appropriate changes can make Blocker to quit unexpectedly after the catch.

Another method is given by the auto-restart command line.

If a thread to run the following code

            int a = 0;
            int c = 10 / a;

It will cause the program to automatically end, and no message but if you run this code in the main thread, a dialog box will pop exception information

How this exception Info dialog box also appears in the thread. Or avoid the program to exit , ignore the exception, continue to enforce it down?
 
In the main thread WINFORM capture all the anomalies on the line, the following code:
            // processing uncaught exception 
            Application.SetUnhandledExceptionMode (UnhandledExceptionMode.CatchException);
             // process the UI thread exception 
            Application.ThreadException + = new new System.Threading.ThreadExceptionEventHandler (Application_ThreadException);
             // process the non-UI thread exception 
            AppDomain.CurrentDomain.UnhandledException + = new new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException);
 Most frequently occurring errors in: UnhandledException appear in. Detailed code is as follows:
Copy the code
        ///  <Summary> 
        /// main entry point for the application.
        ///  </ Summary> 
        [STAThread]
         static  void the Main ( String [] args) 
        { 
            Application.EnableVisualStyles (); 
            Application.SetCompatibleTextRenderingDefault ( to false ); 

            // process uncaught 
            Application.SetUnhandledExceptionMode (UnhandledExceptionMode.CatchException);
             / / processing UI thread exception 
            Application.ThreadException + = new new System.Threading.ThreadExceptionEventHandler (Application_ThreadException);
             // process non-UI thread exception
            + = AppDomain.CurrentDomain.UnhandledException new new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException); 

            the Application.Run ( new new the Form1 (args)); 
            glExitApp = to true ; // Flag application can exit 
        } 

        ///  <Summary> 
        /// whether to exit the application
         / //  </ Summary> 
        static  BOOL glExitApp = to false ; 

        static  void CurrentDomain_UnhandledException ( Object SENDER, UnhandledExceptionEventArgs E) 
        { 
            LogHelper.Save ( "CurrentDomain_UnhandledException " , LogType.Error); 
            LogHelper.Save ( " IsTerminating: " + e.IsTerminating.ToString (), LogType.Error); 
            LogHelper.Save (e.ExceptionObject.ToString ()); 

            the while ( to true ) 
            { // cycle, otherwise the application will quit 
                IF (glExitApp) { // mark application may quit, or quit the program, the process is still running 
                    LogHelper.Save ( " ExitApp " );
                     return ; 
                } 
                System.Threading.Thread.Sleep ( 2 *1000);
            };
        }
        
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogHelper.Save("Application_ThreadException:" +
                e.Exception.Message, LogType.Error);
            LogHelper.Save(e.Exception);
            //throw new NotImplementedException();
        }
Copy the code

 If you need to restart the program only needs to start the current application code to capture the event in the process. Refer to the following:

Copy the code
CmdStartCTIProc (Application.ExecutablePath, " cmd the params " ); // post processing code into a capture event, restart the program, plus the parameters needed to restart the 


        ///  <Summary> 
        /// executed in the command line window
         // /  </ Summary> 
        ///  <param name = "sExePath"> </ param> 
        ///  <param name = "sArguments"> </ param> 
        static  void CmdStartCTIProc ( String sExePath, String sArguments) 
        { 
            Process P = new new Process (); 
            p.StartInfo.FileName = " cmd.exe " ;
            P.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = false;
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            p.Start();
            p.StandardInput.WriteLine(sExePath + " " + sArguments);
            p.StandardInput.WriteLine ( " Exit " );
            p.Close (); 

            System.Threading.Thread.Sleep ( 2000 ); // must wait, start or restart the program has not been completed; adjust according to the situation latency 
        }
Copy the code

 Another way to restart the process:

            // restart the program, plus the time required to restart the parameter 
            System.Diagnostics.ProcessStartInfo CP = new new System.Diagnostics.ProcessStartInfo (); 
            cp.FileName = Application.ExecutablePath; 
            cp.Arguments = " cmd the params " ; 
            cp.UseShellExecute = to true ; 
            the System.Diagnostics.Process.Start (CP);

Friends looked feel useful, if your convenience, you can look at the top. Thank you!

 

 

Source: https://www.cnblogs.com/zaspring/archive/2013/04/16/3023927.html

This article reference information online search, and make appropriate changes can make Blocker to quit unexpectedly after the catch.

Another method is given by the auto-restart command line.

If a thread to run the following code

            int a = 0;
            int c = 10 / a;

It will cause the program to automatically end, and no message but if you run this code in the main thread, a dialog box will pop exception information

How this exception Info dialog box also appears in the thread. Or avoid the program to exit , ignore the exception, continue to enforce it down?
 
In the main thread WINFORM capture all the anomalies on the line, the following code:
            // processing uncaught exception 
            Application.SetUnhandledExceptionMode (UnhandledExceptionMode.CatchException);
             // process the UI thread exception 
            Application.ThreadException + = new new System.Threading.ThreadExceptionEventHandler (Application_ThreadException);
             // process the non-UI thread exception 
            AppDomain.CurrentDomain.UnhandledException + = new new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException);
 Most frequently occurring errors in: UnhandledException appear in. Detailed code is as follows:
Copy the code
        ///  <Summary> 
        /// main entry point for the application.
        ///  </ Summary> 
        [STAThread]
         static  void the Main ( String [] args) 
        { 
            Application.EnableVisualStyles (); 
            Application.SetCompatibleTextRenderingDefault ( to false ); 

            // process uncaught 
            Application.SetUnhandledExceptionMode (UnhandledExceptionMode.CatchException);
             / / processing UI thread exception 
            Application.ThreadException + = new new System.Threading.ThreadExceptionEventHandler (Application_ThreadException);
             // process non-UI thread exception
            + = AppDomain.CurrentDomain.UnhandledException new new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException); 

            the Application.Run ( new new the Form1 (args)); 
            glExitApp = to true ; // Flag application can exit 
        } 

        ///  <Summary> 
        /// whether to exit the application
         / //  </ Summary> 
        static  BOOL glExitApp = to false ; 

        static  void CurrentDomain_UnhandledException ( Object SENDER, UnhandledExceptionEventArgs E) 
        { 
            LogHelper.Save ( "CurrentDomain_UnhandledException " , LogType.Error); 
            LogHelper.Save ( " IsTerminating: " + e.IsTerminating.ToString (), LogType.Error); 
            LogHelper.Save (e.ExceptionObject.ToString ()); 

            the while ( to true ) 
            { // cycle, otherwise the application will quit 
                IF (glExitApp) { // mark application may quit, or quit the program, the process is still running 
                    LogHelper.Save ( " ExitApp " );
                     return ; 
                } 
                System.Threading.Thread.Sleep ( 2 *1000);
            };
        }
        
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogHelper.Save("Application_ThreadException:" +
                e.Exception.Message, LogType.Error);
            LogHelper.Save(e.Exception);
            //throw new NotImplementedException();
        }
Copy the code

 If you need to restart the program only needs to start the current application code to capture the event in the process. Refer to the following:

Copy the code
CmdStartCTIProc (Application.ExecutablePath, " cmd the params " ); // post processing code into a capture event, restart the program, plus the parameters needed to restart the 


        ///  <Summary> 
        /// executed in the command line window
         // /  </ Summary> 
        ///  <param name = "sExePath"> </ param> 
        ///  <param name = "sArguments"> </ param> 
        static  void CmdStartCTIProc ( String sExePath, String sArguments) 
        { 
            Process P = new new Process (); 
            p.StartInfo.FileName = " cmd.exe " ;
            P.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = false;
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            p.Start();
            p.StandardInput.WriteLine(sExePath + " " + sArguments);
            p.StandardInput.WriteLine ( " Exit " );
            p.Close (); 

            System.Threading.Thread.Sleep ( 2000 ); // must wait, start or restart the program has not been completed; adjust according to the situation latency 
        }
Copy the code

 Another way to restart the process:

            // restart the program, plus the time required to restart the parameter 
            System.Diagnostics.ProcessStartInfo CP = new new System.Diagnostics.ProcessStartInfo (); 
            cp.FileName = Application.ExecutablePath; 
            cp.Arguments = " cmd the params " ; 
            cp.UseShellExecute = to true ; 
            the System.Diagnostics.Process.Start (CP);

Friends looked feel useful, if your convenience, you can look at the top. Thank you!

 

Guess you like

Origin www.cnblogs.com/mq0036/p/11502523.html