2

I'm having problems killing a labview process. I'm using this code so far:

string NombreProceso = "FORD EPB FINAL Laser";
Process[] ps = Process.GetProcessesByName(NombreProceso);
if (ps.Count() == 0)
{
     TextBox_Eventos.AppendText("[+]   " + DateTime.Now.ToString("hh:mm:ss") + " | No se encontró proceso\n");
     return;
}

foreach (Process p in ps)
{
     p.kill();

     TextBox_Eventos.AppendText("[+]   " + DateTime.Now.ToString("hh:mm:ss") + " | Cerrando proceso\n");
}

The problem is that the process is not finishing (looks like p.kill() is not working). I'm getting the exception: "No process is associated with this object". I'm just looking for a function that helps me kill this process like task manager does.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • 1
    Log how many processes it actually found, if it's more than one you could have a mess where one process communicates with others and makes your collection invalid (e.g. killing one kills the others). Also check PID to confirm 100% that it didn't die, maybe new process spawned to replace the old one. Other than that I can't see anything wrong with the code. – bokibeg Dec 17 '15 at 17:33
  • Just a thought: I would try it with a normal `for` loop with an index variable rather than a `foreach`. – David Tansey Dec 17 '15 at 17:33
  • 3
    maybe the process no longer exists when you run the kill? – Hogan Dec 17 '15 at 17:33
  • Have you attempted to debug it? What does `p` represent at the point of the exception, does it point to the process you want to kill? – CodingGorilla Dec 17 '15 at 17:35
  • 2
    On a side note, this doesn't seem to have anything to do with labview, please consider removing that tag from the question. – CodingGorilla Dec 17 '15 at 17:36
  • How many processes are you finding? Are any of them getting killed? – Matt Burland Dec 17 '15 at 17:43
  • Looks like you are confusing the name that appears on the window with the process name. Use Task Manager, Processes tab. – Hans Passant Dec 17 '15 at 17:46
  • 1
    @HansPassant that could not be the case here, since otherwise GetProcessesByName would return an empty array. – Camilo Terevinto Dec 17 '15 at 18:11
  • I think the possible reason is that I have multiple process with that name. Now the cuestion is How do I kill all those process? If they exist of course. – Sergio Antonio Marin Velazquez Dec 17 '15 at 18:50

1 Answers1

1

As far as the documentation goes, there is no way to determine whether the object is associated with a process or not. The only way to do what you want is wrap the call in a try-catch block:

string NombreProceso = "FORD EPB FINAL Laser";
Process[] ps = Process.GetProcessesByName(NombreProceso);
if (ps.Count() == 0)
{
    TextBox_Eventos.AppendText("[+]   " + DateTime.Now.ToString("hh:mm:ss") + " | No se encontró proceso\n");
        return;
}
foreach (Process p in ps)
{
   try
   {
       p.kill();
   }
   catch (InvalidOperationException) {}

    TextBox_Eventos.AppendText("[+]   " + DateTime.Now.ToString("hh:mm:ss") + " | Cerrando proceso\n");
}

You could also try to see how many handles the object has, since Process.Kill() first needs to get the handle of the object, the object needs to have at least one handle for Process.Kill() not to throw the exception when a handle cannot be acquired, as you can see in the ReferenceSource.

string NombreProceso = "FORD EPB FINAL Laser";
Process[] ps = Process.GetProcessesByName(NombreProceso);
if (ps.Count() == 0)
{
    TextBox_Eventos.AppendText("[+]   " + DateTime.Now.ToString("hh:mm:ss") + " | No se encontró proceso\n");
        return;
}
foreach (Process p in ps)
{
   try
   {
       if (p.Handles > 0)
          p.Kill();
   }
   catch (InvalidOperationException) {}

    TextBox_Eventos.AppendText("[+]   " + DateTime.Now.ToString("hh:mm:ss") + " | Cerrando proceso\n");
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • If you look at the linked page, you'll see that `HasExited` will also throw an `InvalidOperationException` if *There is no process associated with the object.* So presumably, whatever is causing the problem, won't be solved that way. – Matt Burland Dec 17 '15 at 17:42
  • Also, killing an already killed task should give you the message *Cannot process request because the process has exited* – Matt Burland Dec 17 '15 at 17:47
  • @MattBurland see my edit, that's what I could make out from the documentation and the source code – Camilo Terevinto Dec 17 '15 at 18:06