您的位置:首页 > 其它

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

2011-10-04 19:29 197 查看
普通的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: