下面是两个简单的例子,说明在 windows 控制台应用程序中定时器的用法
1、直接声明回调函数,然后在回调函数中处理消息。
// programe for timer
#include "stdio.h"
#include "conio.h"
#include <Windows.h>
int count = 0;
void CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
printf("WM_TIMER in work thread count = %d\n",count++);
}
int main()
{
SetTimer (NULL, 0, 1000, TimerProc);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
if(msg.message==WM_TIMER)
{
DispatchMessage(&msg);
if(count == 7)
{
printf("should stop!\n");
break;
}
}
}
KillTimer (NULL, 0);
return 0;
}
2、建立线程,在线程中建立消息处理机制
//programe for timer
#include <windows.h>
#include <stdio.h>
#include <conio.h>
unsigned lo