问题分析以及解决 
 
根据MSDN上面的解释,可以找到以下文字: 
http://msdn.microsoft.com/en-us/ ... 44906(v=vs.85).aspx 
 
The time-out value, in milliseconds. 
If uElapse is less than USER_TIMER_MINIMUM (0x0000000A), the timeout is set to USER_TIMER_MINIMUM. If uElapse is greater than USER_TIMER_MAXIMUM (0x7FFFFFFF), the timeout is set to USER_TIMER_MAXIMUM. 
 
 
Timer使用的时间中断, 
Windows中每隔1/18秒触发一个时钟中断,所以,Timer的定时精度1000/18  
SetTimer线程优先级别很低,要等其它的线程执行完后运行它,所以精度度很差,用于要求不高的场合。如果要求高的话可以采用多媒体定时或更高级别的定时方法。  
 
WM_TIMER消息的优先级比较低,当消息队列里没有其它待处理的高优先级的消息的时候,才会去处理它,如果你的应用程序很繁忙,那么延迟就会很明显。 
The WM_TIMER message is a low-priority message. The GetMessage and PeekMessage functions post this message only when no other higher-priority messages are in the thread's message queue.   
 
可以用以下代码验证: 
[C++] syntaxhighlighter_viewsource syntaxhighlighter_copycode #include <windows.h>
static int g_nCount = 0;
#define Timer_Once_Time (1000/18)
DWORD WINAPI threadFunc (LPVOID pArg)
{
	Sleep(100*Timer_Once_Time);
	printf("%d",*((int*)pArg));
	return 0;
}
void CALLBACK TimerProc(HWND hwnd, UINT message, UINT timerID, DWORD time) 
{ 
	g_nCount++;
	printf("%d\n",g_nCount);
}
int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hThread;
	hThread = CreateThread (NULL, 0, threadFunc, &g_nCount, 0, NULL );
	
	SetTimer(NULL, 0, 10*Timer_Once_Time, TimerProc); 
	// 主消息循环:
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		//if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return (int) msg.wParam;
} 
 |