“Gracefully” killing a process

浪尽此生 提交于 2019-12-04 15:27:27

问题


Right now I am using Process.Kill() to kill a process. Is there a way though, instead of just killing it immediately, that I can like send a message to the process instructing it to close so that it can gracefully clean up and shut down. Basically, I'm looking for the equivlent to just clicking the red X in the upper right hand corner, which I believe DOES send a message to the application requesting a shut down.


回答1:


If the process has a windows interface (as you refer to the red "X"), you can try Process.CloseMainWindow(). If it fails, you can fallback to Process.Kill().




回答2:


Killing can not be graceful, perhaps you can signal the process to commit suicide. For signaling you have many options.

  • SendMessage
  • NamedPipes
  • Named Mutex
  • Sockets



回答3:


It would depend on the process you're killing. As stated at on the relevant page for Process.Kill, "Kill is the only way to terminate processes that do not have graphical interfaces." If there is a graphical window, then go with the answer by lc above; Process.CloseMainWindow functions as the red X you referred to.



来源:https://stackoverflow.com/questions/3411982/gracefully-killing-a-process

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