In this article, refer to the online search information and make appropriate modifications to prevent the program from exiting after an exception is caught.
In addition, the method of automatic restart through the command line is provided.
If the following code is run in a thread
int a = 0; int c = 10 / a;
The program ends automatically without any prompts. However, if the code is run in the main thread, the exception information dialog box is displayed.
How can I see this exception message dialog box in the thread, or avoid the program exiting directly, ignore the exception, and continue to execute? The following code captures all exceptions in the WINFORM main thread:
// Process the uncaptured exception Application. setUnhandledExceptionMode (UnhandledExceptionMode. catchException); // handle the UI thread exception Application. threadException + = new System. threading. threadExceptionEventHandler (Application_ThreadException); // handle non-UI thread exception AppDomain. currentDomain. unhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException );
The most common error occurs in UnhandledException. The Code is as follows:
/// <Summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main (string [] args) {Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); // process uncaptured exception Application. setUnhandledExceptionMode (UnhandledExceptionMode. catchException); // handle the UI thread exception Application. threadException + = new System. threading. threadExceptionEventHandler (Application_ThreadException); // handle non-UI thread exception AppDomain. currentDomain. unhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException); Application. run (new Form1 (args); glExitApp = true; // indicates that the application can exit} /// <summary> /// whether to exit the application /// </summary> static bool glExitApp = 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. predictionobject. toString (); while (true) {// loop processing, otherwise the application will exit if (glExitApp) {// indicates that the application can exit; otherwise, after the program exits, 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 ();}
If the program needs to be restarted, you only need to start the code of the current application when the captured event is processed. Refer to the following:
Using startctiproc (Application. executablePath, "cmd params"); // put it in the event capture processing code, restart the program, add the restart parameter /// <summary> /// in the command line window. /// </summary> /// <param name = "sExePath"> </param> // <param name = "sArguments"> </param> static void initialize startctiproc (string sExePath, string sArguments) {Process p = 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); // wait; otherwise, the restarted program has not been started; adjust the wait time as needed}
Another method is to restart the process:
// Restart the program. If necessary, add the restart parameter System. diagnostics. processStartInfo cp = new System. diagnostics. processStartInfo (); cp. fileName = Application. executablePath; cp. arguments = "cmd params"; cp. useShellExecute = true; System. diagnostics. process. start (cp );
I have read useful friends. If you are convenient, you can try it out. Thank you!