Kill all running cmd processes
-
Friday, April 13, 2012 1:15 PM
My program runs 10 cmd processes, and i want to add a stop button, so when you press it, it kills all 10 cmd processes. I found some tutorials online, but nothing seems to work!
also, is it possible to make it so when you close the window, it kills the the cmd processes as well?
Thanks a lot :)
- Edited by Monster- Friday, April 13, 2012 1:17 PM
All Replies
-
Friday, April 13, 2012 1:24 PM
It really depends on how you're starting your processes. Why haven't you posted your failing code, so that we don't have to guess? Posting concise and complete examples is in almost any case a good idea.
A look in my Crystal Ball (TM): Thou shall kill it. Use Process.Kill.
-
Friday, April 13, 2012 1:46 PM
> My program runs 10 cmd processes, and i want to add a stop button, so when you press it, it kills all 10 cmd processes.
this is possible with WMI. try using the following code:
using System.Diagnostics; using System.Management; // System.Management.dll using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { void Button_Click(object sender, System.EventArgs e) { TerminateChildProcess(Process.GetCurrentProcess().Id); } static void TerminateChildProcess(int parentId) { var mos = new ManagementObjectSearcher( string.Format("SELECT * FROM Win32_Process WHERE ParentProcessId = {0}", parentId)); foreach (ManagementBaseObject mbo in mos.Get()) { var p = mbo as ManagementObject; if (p == null) continue; uint res = (uint)p.InvokeMethod("Terminate", new object[0]); if (res != 0) { // handle error } } } } }
you can find the other filter criteria in the Win32_Process
- Edited by Malobukv Friday, April 13, 2012 2:00 PM
- Proposed As Answer by Fuzzy.Logician Friday, April 13, 2012 3:33 PM
- Marked As Answer by Alan_chenModerator Tuesday, April 24, 2012 6:10 AM
-
Saturday, April 14, 2012 6:44 AM
Here is the start button,
private void button1_Click(object sender, EventArgs e) { for (int num = 0; num < 10; num++) { Process.Start("buddy.bat"); } }
and here is the stop button that wont work,
private void button1_Click_1(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("Windows Command Prompt");
foreach (Process process in processes)
{
process.Kill();
}
}- Edited by Monster- Saturday, April 14, 2012 6:45 AM
-
Sunday, April 15, 2012 3:06 AM
Windows Command Prompt should be "cmd". This should work:
Array.ForEach(Process.GetProcessesByName("cmd"), x => x.Kill());
Cheers :)If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum- Marked As Answer by Alan_chenModerator Tuesday, April 24, 2012 6:09 AM
-
Monday, April 16, 2012 2:16 PM
Okay, when no one's asking: I translate that his program starts the processes, thus when using the Process class the code should simply look like that:
private List<Process> processes = new List<Process>(); private void btnStart_Click(object sender, EventArgs e) { Process process = new Process(); // Setup process. process.Start(); processes.Add(process); // Repeat as neeeded: process = new Process(); // Setup process. process.Start(); processes.Add(process); } private void btnStop_Click(object sender, EventArgs e) { foreach (Process process in this.processes) process.Kill(); }
- Marked As Answer by Alan_chenModerator Tuesday, April 24, 2012 6:10 AM
-
Tuesday, April 17, 2012 3:05 AMModerator
Hi Monster,
I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Tuesday, April 17, 2012 11:25 PM
I have tried this method, but when i press the button, nothing happens?Windows Command Prompt should be "cmd". This should work:
Array.ForEach(Process.GetProcessesByName("cmd"), x => x.Kill());
Cheers :)
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum -
Wednesday, April 18, 2012 1:25 AMModerator
Hi Monster,
Thanks for your feedback.
I think you should debug your code to see what happened.
private void button1_Click_1(object sender, EventArgs e) { Process[] processes = Process.GetProcessesByName("cmd"); // set break point here foreach (Process process in processes) { process.Kill(); } }1.Set break point before foreach line; 2. press F5 to begin debug; 3. open watch window from Debug-Windows-Watch; 4. Input "processes" to watch is there any processes in the value.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

- Marked As Answer by Alan_chenModerator Tuesday, April 24, 2012 6:09 AM
-
Wednesday, April 18, 2012 5:09 PMYou should really tell us (show us your code), how you start your processes ("My program runs 10 cmd processes"). You should already have the necessary information at hand when starting your processes. Just use it. Take a look at my code example.
-
Friday, April 20, 2012 12:57 AM
By "cmd processes" I assumed he meant cmd.exe. My method works, as i've tested it, so there has to be something that we're not understanding here. My method essentially works the same way Alan_chen had posted, only in a more compact way. I would be curious to see what process he's actually trying to kill here, as the only thing that would need changing in the code I gave him (or most others here) would be the string argument input for the GetProcessesByName function.
I would run this:
Process.GetProcessesByName("cmd").Count();
And see if that returns a value greater than 0.
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum
- Edited by AceInfinityMVP Friday, April 20, 2012 12:57 AM
- Edited by AceInfinityMVP Friday, April 20, 2012 12:58 AM
- Marked As Answer by Alan_chenModerator Tuesday, April 24, 2012 6:09 AM

