/*********************************************************************************
* author: hjjdebug
* date: 2011
* description: windows控制台程序使用TIMER
********************************************************************************/
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int g_exit=FALSE;
VOID CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT_PTR id,DWORD dwTime)
{
cout << "Timer Proc" << endl;
}
BOOL WINAPI CtrHandler(DWORD CtrlType)
{
if(CtrlType == CTRL_C_EVENT || CtrlType==CTRL_BREAK_EVENT)
{
cout << "receive CTRL_C event" << endl;
// PostQuitMessage(0); 奇怪:这两个函数在这里均不能实现令消息循环退出
// SendMessage(NULL,WM_QUIT,0,NULL);
g_exit=TRUE; // 所以我这里设置一个全局变量完成通讯。
}
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
BOOL bRet;
MSG msg;
SetConsoleCtrlHandler(CtrHandler,TRUE);
int timerid = SetTimer(NULL,0,1000,TimerProc);
while( (bRet = GetMessage(&msg,NULL,0,0))!= 0)
{
if(g_exit) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
KillTimer(NULL,timerid);
SetConsoleCtrlHandler(CtrHandler,FALSE);
system("pause");
return 0;
}