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!\n");
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.\n"));
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.\n"));
exitFlag = TRUE;
Sleep(4000); /* Decrease this time to get a different effect */
_tprintf(_T("Leaving handler in 1 second or less.\n"));
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.\n"));
exitFlag = TRUE;
Sleep(4000); /* Decrease this time to get a different effect */
_tprintf(_T("Leaving handler in 1 second or less.\n"));
return TRUE; /* Try returning FALSE. Any difference? */
default:
_tprintf(_T("Event: %d received by handler. Leaving in 5 seconds or less.\n"), cntrlEvent);
exitFlag = TRUE;
Sleep(4000); /* Decrease this time to get a different effect */
_tprintf(_T("Leaving handler in 1 seconds or less.\n"));
return TRUE; /* TRUE indicates that the signal was handled. */
}
}
最新文章
- python中获取今天昨天和明天的日期
- CK-Editor content.replace
- jQuery Mobile学习之grid、等待显示的ajax效果、页面跳转、页面跳转传递参数等(二)
- Codeforces 617E XOR and Favorite Number(莫队算法)
- AOP 之 6.1 AOP基础(拾陆)
- 如何在linux中配置PHP环境
- 使用NPOI导出DataTable到Excel
- linux中的常用命令
- Break、continue、return用法(C++)
- OpenNebula openldap集成
- OutputDebugString输出调试信息到debugtrack
- 【Java】0X001.配置开发环境,JDK、classpath等
- centos 桌面没有有线设置,不能上网
- 通过类创建子线程&;同步锁
- Hibernate常用API以及使用说明
- 在notepad++中使用正则匹配功能(一-龥!-~) 中文[利刃篇]
- php苹果内购订单验证
- IE DIV背景透明,点击事件不响应解决方案
- android studio(AS) Duplicate files copied in APK META-INF/NOTICE.txt
- [luogu3942] 将军令