How to Kill a Process in C#
While developing FastF12 I wanted to develop a feature that would allow the user to cancel and pause the render. It took me a while to find out how to kill a process in C#, so I though I would do a simple tutorial on it. The simple code is below:
string processName = "Process Name";
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process process in processes) {
process.Kill();
}
You replace Process Name with the process name you want to kill, and the program will kill any process with that name. For example, for my program I set processName = "blender", and it killed any instance blender.exe .
string processName = "blender";
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process process in processes) {
process.Kill();
}
If you are not exactly sure what the name of the process you are trying to kill just check Processes in the Windows Task Manager(ATL+CTRL+DEL).
I hope this helps anyone that was trying to accomplish this. Don’t forget to subscribe via RSS or Email if you liked this post.
|
June 19th, 2009 at 5:09 pm
cool thx
December 12th, 2009 at 10:30 pm
And in C++ on Linux/UNIX it’s as easy as:
static void kill() { system("killall blender"); }