Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the handling of the vc console program shutdown event?

Shulou Source: shulou.com Published: 2022-06-02 08:01:01 09月20日 Update

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!

Tags: Console control tag code state thread event function more knowledge deadlock aftermath output mode program processing content practical beautiful comfortable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi Shulou Information MySQL vpn