How to kill a spawned process?

Posted on 2008-06-15
Last Modified: 2010-04-21

I'm running an external server using System.Diagnostics.Process, the problem
is that this server spawns another process.
So far I am only able to close/kill the first process but not the spawned one.
Is there some way to track all spawned processes and kill them on demand?

I can not kill it by name alone, since this is a common thread name, which could
mean that I kill some other apps process.

Question by:oxyoo
  • 2
LVL 10

Author Comment

ID: 21790774
I think I have what is needed now, after a lot of research. The code
is pretty ugly but so far does the job.

Can anyone find any flaws with it?

What the code does is that iterates through all the processes and
finds the parentId, then compares it with my runningServer process
to see if it is the parent. If yes, it kills it.

                int parentId = runningServer.Id;

                foreach (Process process in Process.GetProcesses()) {

                    PerformanceCounter myParentMultiProcID = new PerformanceCounter("Process", "ID Process",


                    try {

                        float tmpParentPID = myParentMultiProcID.NextValue();

                        if (tmpParentPID == parentId) {





                    catch (InvalidOperationException) {}


                // NOT NEEDED?

                try {



                    runningServer = null;


                catch (InvalidOperationException) {}

Open in new window


Accepted Solution

gregmulvihill earned 500 total points
ID: 21791840
I adapted a solution ( presented by Robert Villahermosa to deal with multiple process instances of the same name.  Pass in the process ID of the server you started with System.Diagnostics.Process, and FindChildProcessIDs should return an array with all the spawned process IDs.  Then you can iterate through the list and kill the processes.


int[] oChildProcessIDs = FindChildProcessIDs(nServiceProcessID);

foreach (int nProcessID in oChildProcessIDs)
      Process oProcess = Process.GetProcessById(nProcessID);

		public static int[] FindChildProcessIDs(int nParentProcessID)


			List<int> oChildProcessIDs = new List<int>();

			foreach (Process oProcess in Process.GetProcesses())


				Process oParentProcess = FindParentProcess(oProcess);

				if ((null != oParentProcess) && (nParentProcessID == oParentProcess.Id))





			return oChildProcessIDs.ToArray();



		public static Process FindParentProcess(Process oTargetChildProcess)


			Process oParentProcess = null;

			Process[] oChildProcessList = Process.GetProcessesByName(oTargetChildProcess.ProcessName);

			for (int nIndex = 0; nIndex < oChildProcessList.Length; nIndex++)


				string sChildProcessName = oTargetChildProcess.ProcessName;

				if (nIndex != 0) sChildProcessName += "#" + nIndex;

				PerformanceCounter oChildProcessID = new PerformanceCounter("Process", "ID Process", sChildProcessName);

				PerformanceCounter oParentProcessID = new PerformanceCounter("Process", "Creating Process ID", sChildProcessName);

				int nChildProcessID = (int)oChildProcessID.NextValue();

				int nParentProcessID = (int)oParentProcessID.NextValue();



				if (oTargetChildProcess.Id == nChildProcessID)




						oParentProcess = Process.GetProcessById(nParentProcessID);



					catch { }



			return oParentProcess;


Open in new window

LVL 10

Author Closing Comment

ID: 31467371
Thanks, an excellent solution!

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now