What happens when you close a c++ console application

别说谁变了你拦得住时间么 提交于 2019-11-26 08:24:43

问题


I guess the question says it all, but, what happens if someone closes a c++ console app? As in, clicks the \"x\" in the top corner. Does it instantly close? Does it throw some sort of exception? Is it undefined behavior?


回答1:


Closing a c++ console app with the "x" in the top corner throws an CTRL_CLOSE_EVENT which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function. In there you could override the close functionality and perform whatever you wished to do, and then optionally still perform the default behavior.




回答2:


I imagine that the console process just gets unceremoniously killed by the OS. If you want to trap this event and do something it looks like the SetConsoleCtrlHandler function is the way to do it.

See also:

  • How to handle a ctrl-break signal in a command line interface
  • Console Event Handling



回答3:


On Linux and other Unix systems, the console runs as a separate process. As you close the shell, it sends the SIGHUP signal to the currently active process or processes that are not executed in the background. If the programmer does not handle it, the process simply terminates. The same signal is sent if you close the SSH session with a terminal and an active process.




回答4:


SIGBREAK is raised on Windows.



来源:https://stackoverflow.com/questions/696117/what-happens-when-you-close-a-c-console-application

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