Peng Lv

毋意,毋必,毋固,毋我。 言必行,行必果。

导航

win32控制台程序中定时器的实现

普通的win32程序中定时器的应用很多也很方便,但是在win32控制台中也是可以使用定时器的,利用的是windows程序的消息循环机制,如下:

#include <iostream>
#include <windows.h>
using namespace std;

void CALLBACK TimeProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime)
{
cout<<"This is a timer."<<endl;
}

/*
* 利用消息循环机制来实现定时器.
*/
int work_2()
{
SetTimer(NULL,1,1000,TimeProc);
MSG msg;
while( GetMessage(&msg,NULL,0,0) )
{
if(msg.message == WM_TIMER)
{
DispatchMessage(&msg);
}
}
return 0;
}

int main()
{
work_2();
return 0;
}


 

posted on 2011-10-04 19:29  Lvpengms  阅读(1771)  评论(0编辑  收藏  举报