在Console中使用Timer (1)

本文内容来自 http://biancheng.dnbcw.info/60/60901.html 是在一个控制台窗口中创建一个线程,然后在线程中处理Timer的消息

整理后的代码如下,VC2008 Express编译通过

#include “stdafx.h”
#include
#include
#include

int count =0;

VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
count++;
printf(“WM_TIMER in work thread count=%d\n”,count);
}

DWORD CALLBACK Thread(PVOID pvoid)
{
MSG msg;

UINT timerid=SetTimer(NULL,111,3000,TimerProc);
BOOL bRet;

PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
while((bRet = GetMessage(&msg,NULL,0,0))!= 0)
{
if(bRet==-1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf(“thread end here\n”);
return 0;
}

int main()
{
DWORD dwThreadId;
printf(“Use timer in workthread of console application\n”);
HANDLE hThread = CreateThread(NULL,0,Thread,0,0,&dwThreadId);
_getch();
return 0;
}

运行结果

contimer

下载 ConTimer

发表评论

电子邮件地址不会被公开。 必填项已用*标注