Jump to content
  • Advertisement
Sign in to follow this  
benryves

[.net] Fully terminating Processes?

This topic is 4569 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using a batch script to assemble files in my IDE, and the batch file (.cmd) is run using the Process class. The problem here is that something goes wrong (such as the assembler going into an infinite loop), bailing out with Process.Kill(); doesn't fully work as it only terminates the batch script, and leaves the assembler running. I can't think of a way around this...

Share this post


Link to post
Share on other sites
Advertisement
Presumably what you want to do is kill the entire process tree.

Although there probably isn't a function to do this, you can iterate the process list (obtained using some kernel function or other), and find the processes which are descendants of the one you want rid of, and then kill them all.

I don't think Windows has the notion of a process group, like in Unix, where you can send a signal to the entire group.

Mark

Share this post


Link to post
Share on other sites
Ah, good plan. I'm assuming that the Process/Diagnostics classes can get me some way there..? I'll hunt through MSDN and see what I can find.

Share this post


Link to post
Share on other sites
Windows has no notion of child processes. You will have to have a processhandle or ProcessId or (.NET) Process object to be able to kill the process.

Cheers

Share this post


Link to post
Share on other sites
Windows has no process groups, but it does record parent process IDs.

So, provided there isn't an intermediate process which has subsequently died (i.e. a process is orphaned), you can determine the descendents of a process.

Systems internals "Process explorer" certainly shows parent PIDs.

Mark

Share this post


Link to post
Share on other sites
I gave up trying to find an elegant .NET solution, and ended up using some Win32 API calls (CreateToolhelp32Snapshot, Process32First and Process32Last) to get a list of all running processes, then iterate through them checking their parent's ID; if it matches the process ID of the process I wish to terminate I then connect to that process (creating a new System.Diagnostics.Process from the ID) then kill it.

Share this post


Link to post
Share on other sites
This is a clip from a little application I was working on named ProcessProtect:

Process[] processList = Process.GetProcesses(System.Environment.MachineName);
foreach(Process p in processList)
{
if(p.Id == IdToKill)
p.Kill();
}



If you have a better solution, I'm all ears.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!