c++ - WINAPI exception when calling SetConsoleHandler when closing program
仅有的幸福
仅有的幸福 2017-05-16 13:24:41
0
1
683

I wrote a program that uses multiple threads to obtain some data, and then stores it in an array. Then when I close the console program, an event will be triggered, and then I will write the data in the array to a file in this event. .
But when I run this program for less than 10 minutes, writing files is normal. When I close the program for more than 20 minutes, it will only output some files and then the program will close itself.

The following is the code involving these places:
This is the call to SetConsoleCtrlHandler in the main function

int main(char*argv[], int argc)
{
    //当你关闭的时候做一些处理工作
    SetConsoleCtrlHandler(ConsoleHandler, TRUE);//接受控制台消息,做一些清理工作 
    
    //其他工作
}

This is the specific function implementation

BOOL WINAPI ConsoleHandler(DWORD msgType)
{
    Print("I'm closing\n");
    if (msgType == CTRL_C_EVENT ||msgType == CTRL_CLOSE_EVENT)
    {
        if (g_hTDF)
        {
    
        WriteResult(str);//这里就是我把数组中的内容写入文件的程序

        Print("console Close complete!\n");
        system("pause");
        return TRUE;
    }
 
    return TRUE;
}

Has anyone encountered this problem?
Is this related to the buffer or something? Because intuitively, it may be that more data is received as time goes by.

仅有的幸福
仅有的幸福

reply all(1)
刘奇

CTRL_CLOSE_EVENT has a timeout mechanism, so no matter how long it takes to write data, the program will shut down after 5 seconds.
You can use CTRL_C_EVENT instead.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template