zoukankan      html  css  js  c++  java
  • SetConsoleCtrlHandler演示

    #include "stdafx.h"
    #include <Windows.h>




    static BOOL WINAPI Handler(DWORD cntrlEvent);
    static BOOL exitFlag = FALSE;

    int _tmain(int argc, LPTSTR argv[])

    /* Beep periodically until signaled to stop. */
    {
            /* Add an event handler. */
        if (SetConsoleCtrlHandler(Handler, TRUE)==FALSE)
            printf("Unable to install handler! ");
        
        while (!exitFlag) { /* This flag is detected right after a beep, before a handler exits */
            Sleep(4750); /* Beep every 5 seconds; allowing 250 ms of beep time. */
            Beep(1000 /* Frequency */, 250 /* Duration */);
        }
        _tprintf(_T("Stopping the main program as requested. "));
        return 0;
    }    

    BOOL WINAPI Handler(DWORD cntrlEvent)
    {
        switch (cntrlEvent) {
            /* The signal timing will determine if you see the second handler message */
            case CTRL_C_EVENT:
                _tprintf(_T("Ctrl-C received by handler. Leaving in 5 seconds or less. "));
                exitFlag = TRUE;
                Sleep(4000); /* Decrease this time to get a different effect */
                _tprintf(_T("Leaving handler in 1 second or less. "));
                return TRUE; /* TRUE indicates that the signal was handled. */
            case CTRL_CLOSE_EVENT:
                _tprintf(_T("Close event received by handler. Leaving the handler in 5 seconds or less. "));
                exitFlag = TRUE;
                Sleep(4000); /* Decrease this time to get a different effect */
                _tprintf(_T("Leaving handler in 1 second or less. "));
                return TRUE; /* Try returning FALSE. Any difference? */
            default:
                _tprintf(_T("Event: %d received by handler. Leaving in 5 seconds or less. "), cntrlEvent);
                exitFlag = TRUE;
                Sleep(4000); /* Decrease this time to get a different effect */
                _tprintf(_T("Leaving handler in 1 seconds or less. "));
                return TRUE; /* TRUE indicates that the signal was handled. */
        }
    }

  • 相关阅读:
    KingPaper初探Java之初学者编码遇到的问题
    KingPaper初探redis之redis数据类型解析(String类型)
    KingPaper初探Java之面向对象对象的声名和实例化(一)
    KingPaper初探百度应用之百度地图API
    MYSQL之用户授权
    nginx入门到精通目录
    nginx入门篇负载均衡策略
    nginx入门篇功能特性
    开博啦
    ubuntu14.04下pycharm的安装及破解
  • 原文地址:https://www.cnblogs.com/duyy/p/3659234.html
Copyright © 2011-2022 走看看