VC 控制台下使用定时器
编号 :A3GS_TV201003050005
描述 :
本文以例子代码的形式讲述了VC 中如何在控制台下使用定时器。
技术实现 :
#include <windows.h>
#include <stdio.h>
#include <conio.h>
int count =0;
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
count++;
printf("WM_TIMER in work thread count=%d/n" ,count);
}
DWORD CALLBACK Thread(PVOID pvoid)
{
BOOL bRet;
MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
UINT timerid=SetTimer(NULL,111,3000,TimerProc);
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;
}