ZZZ
ZZZ

Reputation: 275

Get Correct Application Process

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.

Upvotes: 0

Views: 45

Answers (1)

LiverpoolOwen
LiverpoolOwen

Reputation: 816

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;
}

Upvotes: 1

Related Questions