C # Several ways to start external programs and wait for external programs to close

Source: Internet
Author: User

1. Start an external program without waiting for it to exit.

2. Start the external program and wait for it to exit.

3. Start an external program and wait indefinitely for it to exit.

4. Launch an external program to monitor its exit through events.

//using System.Diagnostics;Private stringAppName ="Main.exe";/// <summary>///1. Start an external program without waiting for it to exit/// </summary>Private voidButton1_Click (Objectsender, EventArgs e)    {Process.Start (appName); MessageBox.Show (String.Format ("external Program {0} started to complete! ", This. AppName), This. Text, MessageBoxButtons.OK, messageboxicon.information);}/// <summary>///2. Start an external program and wait for it to exit/// </summary>Private voidButton2_Click (Objectsender, EventArgs e) {    Try{Process proc=Process.Start (appName); if(Proc! =NULL) {Proc. WaitForExit ( the); if(Proc. hasexited) {MessageBox.Show (String.Format ("The external program {0} has exited! ", This. AppName), This.            Text, MessageBoxButtons.OK, messageboxicon.information); }            Else            {                //if the external program does not end the run, it is forcibly terminated. Proc.                Kill (); MessageBox.Show (String.Format ("The external program {0} was forcibly terminated! ", This. AppName), This.            Text, MessageBoxButtons.OK, messageboxicon.exclamation); }        }    }    Catch(ArgumentException ex) {MessageBox.Show (ex). Message, This.    Text, MessageBoxButtons.OK, Messageboxicon.error); }}/// <summary>///3. Start an external program and wait indefinitely for it to exit/// </summary>Private voidButton3_Click (Objectsender, EventArgs e) {    Try{Process proc=Process.Start (appName); if(Proc! =NULL) {Proc.            WaitForExit (); MessageBox.Show (String.Format ("The external program {0} has exited! ", This. AppName), This.        Text, MessageBoxButtons.OK, messageboxicon.information); }    }    Catch(ArgumentException ex) {MessageBox.Show (ex). Message, This.    Text, MessageBoxButtons.OK, Messageboxicon.error); }}/// <summary>///4. Launch an external program to monitor its exit through events/// </summary>Private voidButton4_Click (Objectsender, EventArgs e) {    Try    {    //start an external programProcess proc =Process.Start (appName); if(Proc! =NULL)        {            //Monitoring Process ExitProc. EnableRaisingEvents =true; //specifying Exit Event MethodsProc. Exited + =NewEventHandler (proc_exited); }    }    Catch(ArgumentException ex) {MessageBox.Show (ex). Message, This.    Text, MessageBoxButtons.OK, Messageboxicon.error); }}/// <summary>///To start an external program exit event/// </summary>voidProc_exited (Objectsender, EventArgs e) {MessageBox.Show (String.Format ("The external program {0} has exited! ", This. AppName), This. Text, MessageBoxButtons.OK, messageboxicon.information);}

C # Several ways to start external programs and wait for external programs to close

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.