SoFunction
Updated on 2025-03-06

C# uses Process class to call external program decomposition

In program development, a program often needs to call other programs. The Process class in C# just provides such functions. It provides access to local and remote processes and enables you to start and stop local system processes.

1. Start process instance

Copy the codeThe code is as follows:

Process myProcess = new Process();  
try 
{  
    = false;  
    = "";  
    = true;  
    ();  
}  
catch (Exception e)  
{  
    ();  

Method, start (or reuse) the process resource specified by the StartInfo property of this Process component and associate it with the component. true if process resources are started, and false if new process resources are not started (for example, if existing processes are reused).

Properties, get or set the properties of the Start method to be passed to Process. StartInfo represents a set of parameters used to start a process. When Start is called, StartInfo is used to specify the process to start. The only StartInfo member that must be set is the FileName property.

Properties, get or set the application or document to be launched. The FileName property does not need to represent an executable file. It can be any file type whose extension has been associated with the application installed on the system.

Properties, get or set the value indicating whether to start the process in a new window.

2. Turn off the startup process

Method, immediately stop the associated process. Kill forces the process to be terminated, and the Kill method will be executed asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine whether the process has exited.

3. Call other methods after the process is finished

After calling a process, we usually need to execute other methods. For example, when doing the installer, we need to first determine whether the .Net Framework environment is installed. If it is not installed, we first call the .NET Framework installation program and continue to run the installer method.
Usually in this case, there are two ways:

Method 1: WaitForExit() method. This method blocks the current process until the running external program exits.

Copy the codeThe code is as follows:
exep = (@"C:\Windows\");  
();//Critical, wait for the external program to exit before it can be executed downward.
("Run finished");

After the above code ends, a dialog box pops up.

Method 2: Exited event. Add an event monitor for external processes, and when exiting, get notifications. This method will not block the current process and you can handle other things.

Copy the codeThe code is as follows:
exep = new ();  
= @"C:\Windows\";  
= true;  
+= new EventHandler(exep_Exited);  
();  
 
void exep_Exited(object sender, EventArgs e)  
{  
("Run finished");

After the event is finished, the Exited event method is called.

For the operation of calling external programs in C# program development, it is necessary to determine whether the called EXE file has ended execution or not, in fact, the most consistent application is the class application. One of the methods is WaitForExit(); and HasExited attributes. Both of these are also designed to judge the execution status of the external program exe file. When HasExited=ture, it means the execution end.