Does Process.Start terminate the child program when the parent terminates?

天涯浪子 提交于 2021-01-27 07:58:13

问题


Do programs started with Process.Start(exepath); terminate when the parent process ends? I'm getting some strange behavior with it and believe that could be the issue.


回答1:


The short answer to your question is No, they don't. You will have to kill them explicitly.If you want to kill the process that you have started then you can use the handle returned by process.start. Something like this

Process p = Process.Start("someprocess");
if (p != null)
  p.Kill();



回答2:


On Windows child processes normally live on their own and once started they do not depend on their parent process. You are looking for job objects. With jobs you can control an entire process tree lifetime, all child processes can be deterministically terminated if the parent ends (by having the parent own the job, strictly speaking all child processes terminate if the job is killed). There is no managed .Net API for it, but p-Invoke works fine.

So if you experience unexpected "strange behavior" make sure your process is not launched in the context of a job, causing your child processes to also be part of the job. Process Explorer can show job properties of a process.



来源:https://stackoverflow.com/questions/22318493/does-process-start-terminate-the-child-program-when-the-parent-terminates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!