[Reprint] C # How to call the EXE file and how to determine if the EXE called by the program has ended
Original article address:C # How to call the EXE file and how to determine whether the EXE called by the program has ended
Guoguo
A simple code can call the EXE file in C #, as shown below:
1. Introduce using system. diagnostics; Call code: process. Start (exe file name );
2. Directly system. Diagnostics. process. Start (exe file name ).
C # How to determine if the EXE called by the program has ended
Two Methods: Take the system notepad as an Example
Method 1: This method will block the current process until the running external program exits.
System. Diagnostics. Process exep = system. Diagnostics. process. Start (@ "C: windowsnotepad.exe ");
Exep. waitforexit (); // key. It can be executed only after the external program exits.
MessageBox. Show ("notepad.exe finished running ");
Method 2: Add an event monitor for an external process. After exiting, get the notification. This method does not block the current process. You can handle other tasks.
System. Diagnostics. Process exep = Newsystem. Diagnostics. Process ();
Exep. startinfo. filename = @ "C: \ WINDOWS \ notepad.exe ";
Exep. enableraisingevents = true;
Exep. exited + = new eventhandler (exep_exited );
Exep. Start ();
// Exep_exited event processing Code. The Code is activated after the external program exits and can be executed.
Void exep_exited (Object sender, eventargs E)
{
MessageBox. Show ("notepad.exe finished running ");
}
Determine whether the execution of the EXE is complete and the event listening