Stephen, Killing the parent process will not necessarily terminate all its child processes. Even if your parent is able to somehow detect external termination, it won't be able to kill all its children. The best you could do is terminate the immediate children, if you actually created the child processes, by using the process handles. However, what happens if your children create more child processes? You have no way of knowing that, so you won't be able to kill them either. Basically, there is no parent-child relationship between processes in Windows like traditional Unix environments.
You have to rely on unmanaged Win32 process/threads APIs for this. You have to spawn child processes using "CreateProcess" or "CreateProcessAsUser", and then add your parent process to a Windows JobObject. The JobObject will, from that point onward, keep track of all your child processes. When you decide to cleanup your processes, you can call "TerminateJobObject", which will cleanup your process tree. Mind you that JobObject is available only in Windows 2000, XP and Windows 2003. If you search for JobObject on MSDN, you should find an article about how to use them in MSDN magazine. I use P/Invoke to call the Win32 APIs from C# code. You could do it with Managed C++ or COM. HTH, Srihari On Fri, 6 Aug 2004, Stephen Hess wrote: > Hi all, > > I'm trying to use System::Diagnostics::Process to spin off processes > (mostly doing builds using devenv.com), watch/store the output, and provide > a method for killing that process if desired. > > It appears that when I call Process.Kill - it will kill the direct process > that I spawned, but any child process stays running. The docs make it > sound like it should kill everything, but that's not what I'm seeing. > > Launching the process: > > ... > mRunningProcess->StartInfo->set_FileName( execStr ); > mRunningProcess->StartInfo->set_Arguments( argsStr ); > mRunningProcess->StartInfo->set_UseShellExecute( false ); > mRunningProcess->StartInfo->set_CreateNoWindow( true ); > mRunningProcess->StartInfo->set_RedirectStandardOutput( true ); > mRunningProcess->StartInfo->set_RedirectStandardError( true ); > mRunningProcess->Start(); > ... > > With execStr set to: > "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE\devenv.com" > > And argsStr set to: > "BuildMe.sln" /rebuild Release > > Then killing the process via a button click from the GUI: > > if ( ( mRunningProcess != 0x0 ) && !mProcessBreak ) > { > mProcessBreak = true; > > if ( !mRunningProcess->CloseMainWindow() ) > { > mRunningProcess->Kill(); > } > > Thread::Sleep( 1 ); > } > > Any help very much appreciated. > > Thanks, > Stephen Hess > > =================================== > This list is hosted by DevelopMentorŪ http://www.develop.com > Some .NET courses you may be interested in: > > Essential .NET: building applications and components with CSharp > August 30 - September 3, in Los Angeles > http://www.develop.com/courses/edotnet > > View archives and manage your subscription(s) at http://discuss.develop.com > =================================== This list is hosted by DevelopMentorŪ http://www.develop.com Some .NET courses you may be interested in: Essential .NET: building applications and components with CSharp August 30 - September 3, in Los Angeles http://www.develop.com/courses/edotnet View archives and manage your subscription(s) at http://discuss.develop.com