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