SetTimer 函数在控制台应用程序中的应用 (在控制台应用程序中使用定时器)
示例代码(test.c):
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
VOID CALLBACK TimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
static s_count = 0;
printf("WM_TIMER in work thread s_count = %d\n", ++s_count);
}
DWORD CALLBACK Thread(PVOID pvoid)
{
MSG msg;
BOOL bRet;
UINT timerid;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
timerid = SetTimer(NULL,0, 3000, TimerProc);
while ((bRet = GetMessage(&msg,NULL,0,0))!=0)
{
if (bRet == -1)
{
printf("Error:the thread will quit,error id is %d\n",GetLastError());
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
printf("thread end here\n");
return 0;
}
int main()
{
HANDLE hThread;
printf("use timer in console application\n");
hThread = CreateThread(NULL, 0, Thread, NULL, 0, NULL);
_getch();
return 0;
}