Kill a process only if it's running, without looping through every running process

I want to know how to kill just one process, but I’m getting an exception thrown because the process isn’t running, but if it is I want it to be killed before I try to relaunch the process.

So If the process is running, I want to kill it, then relaunch it.
I put a kill process before I launch it but I’m getting an exception thrown, because the process isn’t actually running. I don’t really want it to continue on error… because what if something else happens. I need to know!

So I don’t want to do get processes, then loop through every single process out of the hundreds I guess that are running on this server. I just want to search for that one process and then kill it if it’s found.

1 Like

Do you know the name of running process that you want to kill.
If know that, could check whether the process is existing in memory before kill.

The name of the process is javaw

Hi there @HsDev,
You can use the following:

strProcessName = "javaw"
If System.Diagnostics.Process.GetProcesses.AsEnumerable().Any(Function (prcRunning) prcRunning.ProcessName.ToLower.Trim.Equals(strProcessName.ToLower.Trim)) Then

  • Kill Process - ProcessName = strProcessName.ToLower.Trim

End If

10 Likes

Can you give me a breakdown of this code? I understand everything up to AsEnumerable(). How is the rest of this working exactly. Just so I have a good understanding of how it works. If you can.

Hi @HsDev
In his example code, .AsEnumerable() just converts the “GetProcesses” to a list so you can query it. .Any() is checking if “any” item in the list matches the condition, and the condition is .Equals("javaw")

so it is returning True or False if the list of processes that are open if it contains “javaw”

If you want, you can take a look at the “Kill Processes per User” snippet I uploaded into Go.
https://go.uipath.com/component/killprocessesperuser

It allows you to tell it which application you want to kill from either a text file or array (ie {“javaw”}).
It will also list all the open processes so you can find the name of it easier. You can also exclude some processes if you use a long list of processes.

I don’t think the code is too complex. It basically just creates the list of processes, filters it to the ones from the processes you want to kill, then kills them.

EDIT: additionally, it kills them per user for server environments so you don’t kill an application being used by another Robot on the same server.

4 Likes

Okay, good to know all that. What about the Function(prcRunning) And then I see that’s somehow accessing a class or something… How does that all work. Or I guess how could I use that in a different example.

The Function(word) part is a way to declare a variable to represent each item within the list you are iterating by the lambda expression/function (ie .Where(), .Any(), .Select(), etc)…
and that variable that is declared is only seen within those parentheses.

So when he used Function(prcRunning), prcRunning represents each item being iterated in the list - it’s basically kind of like a loop but much quicker.

I hope I explained that well. :smiley:

5 Likes

HI @HsDev,

You can also use the below code -
All you need to do is Replace “OUTLOOK” with your required process name “javaw”.

Regards,
Suman

8 Likes

Everyone keeps talking about lambda expressions on the internet. I think I should read up on those. Thanks for clarifying, that does help, and it’s simple to understand.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.