0

I have a C# program and I want only kill one specific process of it for example I have these three instance running at the same time in this way:

  • first application1 use the name.exe

  • 2nd application2 use the name.exe

  • 3rd application3 use the name.exe

when I am close the application1 I used the code for kill the process

Process[] pr = Process.GetProcessesByName("MAPPOINT");
        foreach (Process prs in pr)
        {
            if (prs.ProcessName.ToUpper() == "NAME")
            {
                prs.Kill();
            }

this code kill all name.exe process where I want to kill specific one.

how to kill only one process which is user by application1 not close the application2 and application3 process?

Sirena
  • 9
  • 2
  • How did you specify the one? Or you just need to kill anyone? – shingo Feb 07 '23 at 11:27
  • @shingo I am trying to find a way to specify each one and kill the determined one – Sirena Feb 07 '23 at 11:29
  • Let me ask in another way: If you go to kill it manually, you have opened the task manager, then how do you know which one you want to kill? – shingo Feb 07 '23 at 11:32
  • @shingo I don't know to kill it manually I need that via C# code. – Sirena Feb 07 '23 at 11:36
  • 1
    BTW if the specific process is started by code, you should have an instance of Process, and you just need to call Kill on it. – shingo Feb 07 '23 at 11:36
  • @shingo I have list of users I want to kill a process specified of this user – Sirena Feb 07 '23 at 11:37
  • if you have no idea on how to do it manually, how do you expect any program to guess it? You should definitly try to do it by hand to get an idea of the needed steps. Adterwards you can go and write some code that executes those steps automatically. – MakePeaceGreatAgain Feb 07 '23 at 11:37
  • @shingo lets say we have 5 users on the same pc and I want to kill the process that running by this user – Sirena Feb 07 '23 at 11:38
  • Didn't we see that question appear yesterday(https://stackoverflow.com/questions/75363912/how-to-kill-a-specific-process-of-c-sharp-program?)? Just call `GetProcessByName` and then check for the user of that process using this appraoch: https://stackoverflow.com/questions/777548/how-do-i-determine-the-owner-of-a-process-in-c – MakePeaceGreatAgain Feb 07 '23 at 11:40
  • 1
    You can also try taskkill command, `Process.Start("taskkill", "/FI \"usename eq USER001\"");` – shingo Feb 07 '23 at 11:45

1 Answers1

0

First to Start

If 'application1.exe' is always the first to start, then you could try killing the lowest PID. I believe PIDs are always assigned sequentially, so the lowest number would be the first that was opened.

So something like:

Process[] pr = Process.GetProcessesByName("MAPPOINT");

// check edgecase - no application open with this name...
if (pr.Length < 0)  {  return;  }

//edgecase - give a default value
int firstPID= pr[0].Id;

//iterate through to find lowest number PID
foreach(Process prs in pr)
{
    if (prs.Id < firstPID) 
    { 
        firstPID= prs.Id; 
    }
}

// close the first (aka lowest) PID
Process process = Process.GetProcessById(firstPID);
process.Kill();

If you need to be careful and only close if there are 3 or more application instances running, then put it in a function and check the length:

private static void CloseFirstInstance_IfMoreThanThree()
{

    Process[] pr = Process.GetProcessesByName("MAPPOINT");

    // check edgecase - no application open with this name...
    if (pr.Length < 0) { return; }

    //edgecase - not enough applicaitons open
    if (pr.Length < 3) { return; }

    //edgecase - give a default value
    int firstPID = pr[0].Id;
    
    ...
    
    }
    

Alternates

If the instance you want to kill is not the first to start, you may need to get fancy and:

  1. Ask the user to select the window, then grab the pointer to the window, then grab the PID. (Getting more difficult - UI windows have been a pain for me.)
  2. Actually might be simpler: you might try PID = Process.GetProcessesByName(MyProcessName).FirstOrDefault().Id; and kill that process. This could be all you need.