c# - Process.Kill() doesn't seem to kill the process -
i having trouble using process.kill()
. think must misunderstanding how works. test function. start long-running process (ping -t
) , kill 5 seconds later.
i can see ping process show up, process still there after program finishes. have kill manually.
console.writeline("total number of ping processes {0}", process.getprocessesbyname("ping").length); processstartinfo startinfo = new processstartinfo("cmd.exe"); process process = new process(); startinfo.createnowindow = true; startinfo.useshellexecute = false; startinfo.arguments = "/c ping -t 8.8.8.8"; console.writeline("staring ping process"); process.startinfo = startinfo; process.start(); thread.sleep(5000); console.writeline("total number of ping processes {0}", process.getprocessesbyname("ping").length); thread.sleep(5000); console.writeline("killing ping process"); process.kill(); thread.sleep(5000); console.writeline("total number of ping processes {0}", process.getprocessesbyname("ping").length);
what doing wrong here?
you started cmd.exe, cmd.exe starts child process ping.exe. kill ping.exe can kill process hierarchy. example wmi(add system.management
reference):
private static void killprocessandchildrens(int pid) { managementobjectsearcher processsearcher = new managementobjectsearcher ("select * win32_process parentprocessid=" + pid); managementobjectcollection processcollection = processsearcher.get(); try { process proc = process.getprocessbyid(pid); if (!proc.hasexited) proc.kill(); } catch (argumentexception) { // process exited. } if (processcollection != null) { foreach (managementobject mo in processcollection) { killprocessandchildrens(convert.toint32(mo["processid"])); //kill child processes(also kills childrens of childrens etc.) } } }
Comments
Post a Comment