简体   繁体   English

在WinAPI中为控制台应用程序处理窗口关闭事件的最简单方法是什么?

[英]What is the easiest way to handle window close event in WinAPI for the console application?

I'm writing a console multi-process application in c++ using WinAPI. 我正在使用WinAPI在c ++中编写一个控制台多进程应用程序。 So I have the Dispatcher(eg "Parent") and the Client(eg "Child") processes. 所以我有Dispatcher(例如“Parent”)和Client(例如“Child”)进程。 Both processes are synchronized: they're using semaphors, events, mutexes and the pipe (all of them are standard WinAPI handles). 两个进程都是同步的:它们使用信号量,事件,互斥体和管道(所有这些都是标准的WinAPI句柄)。 Application stop when the user type the "exit" command. 当用户键入"exit"命令时,应用程序停止。 If the user do so, the dispatcher process notifies it's child, and then child releases its resources and makes another before-exit procedures to exit correctly. 如果用户这样做,调度程序进程会通知它的子进程,然后子进程释放其资源并使另一个退出前程序正确退出。 But there's a thing that bothers me: what will happen if the user press the window "close" button? 但是有一件事困扰着我:如果用户按下“关闭”按钮,会发生什么? If so, I should listen to close event and then perform my resource-releasing procedure. 如果是这样,我应该听关闭事件,然后执行我的资源释放程序。 What is the easiest way to handle window close event? 处理窗口关闭事件的最简单方法是什么?

编写一个控制台处理程序例程 ,检测CTRL_CLOSE_EVENT(和CTRL_C_EVENT,如果需要),并使用SetConsoleCtrlHandler将处理程序例程添加到您的进程。

It isn't really different from the client process crashing or being terminated through Task Manager. 它与客户端进程崩溃或通过任务管理器终止并没有什么不同。 You ought to be resilient to that as well. 你也应该对此有弹性。 The ultimate signal you get for that in the parent process is that the client's process handle will be signaled. 您在父进程中获得的最终信号是将发出客户端进程句柄的信号。

Use WaitForMultipleObjects, along with those other handles, to detect this. 使用WaitForMultipleObjects以及其他句柄来检测它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM