How to determine if a process is running, but has not yet quit?

I have code that creates a Process instance and runs it later. There is some kind of logic that should check if the process is running. HasExited can be used to check if a process has been started, but I cannot find a similar function for HasStarted . At first glance, StartTime looked like a good option, but this function will be thrown if the process ends. In addition, the documentation states that StartTime makes sense for running processes.

What is the β€œright” approach for determining if a process was started (it was running, but can exit)?

+4
source share
4 answers

While the methods suggested by others will work, this is not the most efficient way to handle such things. If you keep a loop checking if the process has exited or not, you will spend a lot of system resources. Your concern should be to simply know when the process is ending, and not to sit to check if he has left. So, the right way is to handle events.

The code below explains how to do this using events.

 // Declare your process object with WithEvents, so that events can be handled. private Process withEventsField_MyProcess; Process MyProcess { get { return withEventsField_MyProcess; } set { if (withEventsField_MyProcess != null) { withEventsField_MyProcess.Exited -= MyProcess_Exited; } withEventsField_MyProcess = value; if (withEventsField_MyProcess != null) { withEventsField_MyProcess.Exited += MyProcess_Exited; } } } bool MyProcessIsRunning; private void Button1_Click(System.Object sender, System.EventArgs e) { // start the process. this is an example. MyProcess = Process.Start("Notepad.exe"); // enable raising events for the process. MyProcess.EnableRaisingEvents = true; // set the flag to know whether my process is running MyProcessIsRunning = true; } private void MyProcess_Exited(object sender, System.EventArgs e) { // the process has just exited. what do you want to do? MyProcessIsRunning = false; MessageBox.Show("The process has exited!"); } 

EDIT: Knowing if a process is running or not should be easy as they start the process somewhere in the code. Thus, you can set the flag there and set the value to false when the process exits. I updated the code above to show how easy it is to set such a flag.

+6
source

Search for your process in Process.GetProcesses(); , the list returned by this method gives all the processes currently running on the machine.

+6
source

You can verify that there is at least thread in this process. This will mean that the process is up and running.

Edit:
You can also check the Id process. This will throw an exception if the process is not running.

Edit 2:
In fact, Threads also throws an exception if Id is not set:

 bool ProcessIsRunning(Process p) { bool isRunning; try { isRunning = !p.HasExited && p.Threads.Count > 0; } catch(SystemException sEx) { isRunning = false; } catch(PlatformNotSupportedException pnsEx) { throw; } return isRunning; } 
+2
source

You can use the Process.GetProcesses method (in the System.Diagnostics namespace) to get a list of the processes currently running on the PC.

Process.GetProcessesByName() can also be used to simply get a list of instances of a particular program.

 // Get all instances of Notepad running on the local computer. Process [] localByName = Process.GetProcessesByName("YourProcess"); 
+2
source

Source: https://habr.com/ru/post/1401592/


All Articles