方法一:
SetTimer(NULL, 0, 1000, (TIMERPROC)Timer2Proc);
VOID CALLBACK Timer2Proc(
HWND hWnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
return;
}
方法二:
// DLL中的線程函數可以象這樣使用Timer
UINT ThreadProc(LPVOID)
{
SetTimer(NULL, 1, 5000, NULL);
MSG msg;
// PeekMessage 強制系統為該線程建立消息棧
PeekMessage(&msg, NULL, NULL, NULL, FALSE);
while (GetMessage(&msg, NULL, NULL, NULL))
{
switch (msg.message)
{
case WM_TIMER:
{
// 這里每5秒鐘執行一次
}
break;
}
//TranslateMessage(&msg);
//DispatchMessage(&msg);
}
KillTimer(NULL, 1);
return 0;
}
方法三:
創建一個線程, 反復讀系統時間不就可以了? 如果定時要求不嚴,用Sleep就可以了
UINT TimerThread(LPVOID pama)
{
UINT oldTickCount, newTickCount;
oldTickCount = GetTickCount();
while(TRUE)
{
while(TRUE)
{
newTickCount = GetTickCount();
if(newTickCount - oldTickCount >= 100)
{
oldTickCount = newTickCount;
break;
}
}
TimeProc();
}
return 0;
}
大約每100ms 調用一次TimeProc();