C USES the Process class to call external program decomposition

  • 2020-06-19 11:39:56
  • OfStack

In program development, one program often needs to call another, and the Process class in C# provides just such a function. It provides access to local and remote processes and enables you to start and stop local system processes.

1. Start the process instance


Process myProcess = new Process();  
try 
{  
    myProcess.StartInfo.UseShellExecute = false;  
    myProcess.StartInfo.FileName = "test.exe";  
    myProcess.StartInfo.CreateNoWindow = true;  
    myProcess.Start();  
}  
catch (Exception e)  
{  
    Console.WriteLine(e.Message);  

The Process.Start method starts (or reuses) the process resource specified by the StartInfo property of this Process component and associates it with the component. true if the process resource is started; false if a new process resource is not started (for example, if an existing process is reused).

Process. StartInfo attribute, which gets or sets the property of the Start method to be passed to Process. StartInfo represents the set of parameters used to start the 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 attribute.

ProcessStartInfo.FileName property, which gets or sets the application or document to launch. The FileName attribute does not need to represent an executable. It can be any file type whose extension is already associated with the application installed on the system.

ProcessStartInfo. CreateNoWindow property, which gets or sets the value indicating whether the process should be started in a new window.

2. Turn off the startup process

The Process.Kill method immediately stops the associated process. Kill forces the process to terminate, and the Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method and wait for the process to exit, or check the HasExited property to see if the process has quit.

3. Call other methods after the process finishes

After calling a process, we usually need to perform other methods. For example, when doing setup, we need to determine whether the.Net Framework environment is installed or not.
Usually in this case, there are two ways:

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

System.Diagnostics.Process exep = System.Diagnostics.Process.Start(@"C:\Windows\Notepad.exe");   
exep.WaitForExit();// Critically, wait for the external program to exit before proceeding   
MessageBox.Show("Notepad.exe After running ");

The above code pops up after the end of Notepad.exe Notepad.

Method 2: Exited event. Add an event monitor for an external process, get notifications when it exits, this method does not block the current process, you can do other things.

System.Diagnostics.Process exep = new System.Diagnostics.Process();   
exep.StartInfo.FileName = @"C:\Windows\Notepad.exe";  
exep.EnableRaisingEvents = true;  
exep.Exited += new EventHandler(exep_Exited);  
exep.Start();  
 
void exep_Exited(object sender, EventArgs e)  
{  
MessageBox.Show("Notepad.exe After running ");  

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

For the operation of calling external program in C# program development, it is the application of System.Diagnostics.Process class to determine whether the called EXE file has finished execution. And the HasExited attribute, both of which are designed to determine the execution status of the external program exe file, when HasExited=ture indicates the end of execution.


Related articles: