Sign in to follow this  

[.net] Fully terminating Processes?

This topic is 4354 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
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
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
Quote:
Original post by Rob Loach
This is a clip from a little application I was working on named ProcessProtect:
*** Source Snippet Removed ***
If you have a better solution, I'm all ears.

That's pretty much what mine does, except that it uses the Win32 API calls I listed in my previous post to check against the parent ID (not the child as yours shows), then add the process's own ID to an ArrayList of tasks to kill. Finally, I go through the list, and do this:
foreach (int ProcessID in ProcessesToKill) {
using (Process ToKill = Process.GetProcessById(ProcessID)) {
ToKill.Kill();
}
}

Share this post


Link to post
Share on other sites

This topic is 4354 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this