Transfer C#winform Program exception exit capture, resume, and auto restart

Source: Internet
Author: User

Special statement: This article is reproduced, here to the original author express deep gratitude! Solve one of our big problems!

This article refers to the online search information, and made the appropriate changes can be caught after the exception to prevent the program to quit.

The method of automatic restart via command line is also given.

If you run the following code in a thread

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

Will cause the program to end automatically without any hint, but if you run the code in the main thread, it will pop up the exception message dialog box.

How to online thread also appear this exception message dialog box. Or avoid the program to exit directly, ignoring the exception, continue to execute? The WinForm main thread captures all exceptions on the line, as in the following code:
            Handling uncaught exception            Application.setunhandledexceptionmode (unhandledexceptionmode.catchexception);            Handling UI Thread Exceptions            application.threadexception + = new System.Threading.ThreadExceptionEventHandler (application_ threadexception);            Handling non-UI thread exceptions            AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (currentdomain_ UnhandledException);
The most common errors appear in the UnhandledException: The detailed code is as follows:
        <summary>///The main entry point of the application. </summary> [STAThread] static void Main (string[] args) {Application.enablevisu            Alstyles ();            Application.setcompatibletextrenderingdefault (FALSE);            Handling uncaught exception Application.setunhandledexceptionmode (unhandledexceptionmode.catchexception); Handling UI Thread Exceptions Application.ThreadException + = new System.Threading.ThreadExceptionEventHandler (application_threadex            Ception); Handling non-UI thread exceptions AppDomain.CurrentDomain.UnhandledException + = new Unhandledexceptioneventhandler (Currentdomain_unha            Ndledexception);            Application.Run (New Form1 (args)); Glexitapp = true;//flag application can exit}///<summary>//whether to quit the application///</summary> S        tatic bool Glexitapp = false; static void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) {LOGHELPER.S Ave("Currentdomain_unhandledexception", logtype.error);            Loghelper.save ("isterminating:" + e.isterminating.tostring (), logtype.error);            Loghelper.save (E.exceptionobject.tostring ());                    while (true) {//loop processing, otherwise the application will exit if (Glexitapp) {//Flag application can exit, otherwise the process is still running after the program exits                    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 requires a restart, only the code of the current application needs to be started when the captured event is processed. Refer to the following:

Cmdstartctiproc (Application.executablepath, "cmd params");//After the processing code of the capture event, restart the program, plus the parameters of the restart when needed///<summary> Execute in command line window///</summary>//<param name= "Sexepath" ></param>//<param Nam            E= "sarguments" ></param> static void Cmdstartctiproc (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);//must wait, otherwise the restart program is not startedTo adjust the wait time according to the situation} 

Another way to restart a process:

            Restart the program, add the parameters of the reboot when required            System.Diagnostics.ProcessStartInfo CP = new System.Diagnostics.ProcessStartInfo ();            Cp. FileName = Application.executablepath;            Cp. Arguments = "cmd params";            Cp. UseShellExecute = true;            System.Diagnostics.Process.Start (CP);

Transfer C#winform Program exception exit capture, resume, and auto restart

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.