开发者

"Gracefully" killing a process

开发者 https://www.devze.com 2023-01-10 12:56 出处:网络
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 c

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.


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().


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号