In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to handle the vc console program shutdown event". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Key console API function: SetConsoleCtrlHandler
In compilers that support C++ 11 or above, you can do this.
SetConsoleCtrlHandler ([] (DWORD fdwctrltype)-> BOOL {if (fdwctrltype = = CTRL_CLOSE_EVENT) {/ / your aftercare code. Return TRUE;} return FALSE;}, TRUE)
It was comfortable to do this at first, but then the problem was discovered:
When the Windows console is in the marked state, output functions such as printf will block when the tag is selected (click the upper left corner of the console-Edit-tag).
As a result, there may be deadlocks in our aftermath code, such as if you want to gracefully end a thread that printf at the last time.
The printf waits for the marking status in the thread, and the SetConsoleCtrlHandler callback function waits for the thread to finish. In short, it is a deadlock.
I wanted to find a console API that could get this tag status, but it didn't work out for a long time.
Finally, I wonder if there is a way to keep printf from deadlock with the tag state. The answer is: redirect the output stream.
So, the code looks like this:
SetConsoleCtrlHandler ([] (DWORD fdwctrltype)-> BOOL {if (fdwctrltype = = CTRL_CLOSE_EVENT) {char szbuf [0x1000]; setvbuf (stdout, szbuf, _ IOFBF, 0x1000); / / your aftermath code. Return TRUE;} return FALSE;}, TRUE)
After doing so, the world is a better place, and if in the end these log messages are important to you, then you may need to write more code to implement them.
Knowledge point expansion:
Example:
BOOL WINAPI ConsoleHandler (DWORD CEvent) {DWORD e = 0bot switch (CEvent) {case CTRL_C_EVENT:e = CTRL_C_EVENT;break;case CTRL_BREAK_EVENT:e = CTRL_BREAK_EVENT;break;case CTRL_CLOSE_EVENT:e = CTRL_CLOSE_EVENT;break;case CTRL_LOGOFF_EVENT:break;case CTRL_SHUTDOWN_EVENT:break;} return true;} int main (int argc, char* argv []) {if (SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ConsoleHandler, TRUE) = = FALSE) {/ / installation failed return-1 } GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0); / / manually generate an event} "what is the handling of the vc console program shutdown event?" that's it. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.