Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Created October 17, 2014 07:12
Show Gist options
  • Save yohhoy/484f82617965a09a7202 to your computer and use it in GitHub Desktop.
Save yohhoy/484f82617965a09a7202 to your computer and use it in GitHub Desktop.
kill process tree(process group) on WIndows/C#
void KillProcessTree(System.Diagnostics.Process process)
{
string taskkill = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "taskkill.exe");
using (var procKiller = new System.Diagnostics.Process()) {
procKiller.StartInfo.FileName = taskkill;
procKiller.StartInfo.Arguments = string.Format("/PID {0} /T /F", process.Id);
procKiller.StartInfo.CreateNoWindow = true;
procKiller.StartInfo.UseShellExecute = false;
procKiller.Start();
procKiller.WaitForExit(1000); // wait 1sec
}
}
@tokit3mx
Copy link

Useless. By doing it using windows's executables, you don't get full control over the output, etc. Sometimes, you want to know if the process that you executed the "termination" function on, died by the function, process, or it died by itself because other process children/thread died. Basically, you have a veery limited amount of control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment