0

How can I get an application's process if more than one instance is open.

var processList= Process.GetProcessesByName("MyProcess"); 

Example:-

2 chrome browsers are open. In the list of processes in the tast mgr, I have 2 chrome processes. I run an application that starts chrome using some Apis "not through Process.Start("")". Now I have 3 in the Task Mgr porcesses

How can I get the process that my application started, and not the other 2 that were already open ? how can I distinguish between the 3 processes that I will get from the statement above

I tried sorting them based on the TotalProcessorTime property, and get the one with the shortest time, but what if I decided to a open another chrome after that, I will get the wrong process.

4
  • What do you mean correct process? So you mean if a process has the same name Commented Apr 28, 2017 at 23:01
  • Yes if I have 3 porcesses, 2 that have been started earlier. Then I started the new process, and I want to get the one I started. All of them have the same name, its the same application Commented Apr 28, 2017 at 23:04
  • When you start the process in your program, you should also have a Process object associated with this process. (If not, use one of the static Process.Start(...) overloads, which return a Process_object). This _Process object will give you the process id and other information about the process you started... Commented Apr 28, 2017 at 23:25
  • I have updated my answer Commented Apr 28, 2017 at 23:36

1 Answer 1

1

Ok based on the information you have given if it is always the most recent Process then you could try the following

MostRecentlyStartedProcess(Process.GetProcessesByName("MyProcess")); public Process MostRecentlyStartedProcess(Process[] procceses) { Process result = null; foreach (Process process in procceses) { if (result == null) { result = process; } else { if (process.StartTime < result.StartTime) { result = process; } } } return result; } 
Sign up to request clarification or add additional context in comments.

1 Comment

your answer will get the current newest process, but what if the user were to start a new process (double clicking chrome) after I already started the program. It would get the wrong process, this is where my issue is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.