关于SetTimer函数的总结
關(guān)于SetTimer函數(shù)的總結(jié)
在使用 SOUI 編寫Windows程序時,似乎曾經(jīng)看到過 SOUI 中限制了 SetTimer 函數(shù)能啟動的定時器的個數(shù),于是今天就去 MSDN 看了下Windows本身是否也限制了單個程序能啟動的定時器個數(shù),于是就有了這篇總結(jié)。
先下結(jié)論好了:微軟在 MSDN 上并沒有寫明單個程序能啟動的定時器的個數(shù)。
但是,還是學(xué)到了不少關(guān)于使用 SetTimer 函數(shù)時之前所不知道的地方,下面就來說一說。
慣例,先上 MSDN 關(guān)于 SetTimer 函數(shù)的原型。
UINT_PTR SetTimer(HWND hWnd,UINT_PTR nIDEvent,UINT uElapse,TIMERPROC lpTimerFunc );那么在使用 SetTimer 函數(shù)時需要注意的地方是什么呢。
在 MSDN 中關(guān)于 nIDEvent 參數(shù)有這樣的說明:
A nonzero timer identifier. If the hWnd parameter is NULL, and the nIDEvent does not match an existing timer then it is ignored and a new timer ID is generated. If the hWnd parameter is not NULL and the window specified by hWnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored. If the call is not intended to replace an existing timer, nIDEvent should be 0 if the hWnd is NULL
上面這段話說了三點(diǎn):
也就是說,當(dāng) hWnd 為 NULL 時,無論第二個參數(shù)傳遞的是什么數(shù)值(傳遞 NULL也行),系統(tǒng)啟動的都不是你所指定的那個定時器,所以需要自己手動保存好 SetTimer 函數(shù)返回的數(shù)值,留待以后調(diào)用 KillTimer 函數(shù)時使用。
而且還要說一點(diǎn):當(dāng) hWnd 為 NULL 時,調(diào)用線程還是會收到 WM_TIMER 消息的,但是 GetMessage 或 PeekMesage 獲取到的 MSG 數(shù)據(jù)中的 hWnd 為 NULL(跟 PostThreadMessage 同理)。所以默認(rèn)情況下,在MFC中使用 SetTimer(NULL, uElapse, NULL) 或 ::SetTimer(NULL, NULL, uElapse, NULL) 時,在 OnTimer 函數(shù)中都是無法響應(yīng)該定時器的,需要重寫MFC的 PreTranslateMessage 函數(shù)才能響應(yīng)該定時器。
關(guān)于 lpTimerFunc 參數(shù),MSDN 的說法也值得注意:
An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER.
就是說:當(dāng) lpTimerFunc 不為NULL時,窗口過程響應(yīng)WM_TIMER時會執(zhí)行該函數(shù),并不會執(zhí)行默認(rèn)的窗口過程。
所以,在MFC中,如果不想重寫MFC的 PreTranslateMessage 函數(shù)的話,可以設(shè)定 lpTimerFunc 參數(shù),這樣也能處理 hWnd 為 NULL 的 SetTimer函數(shù)了。
好了,上面就是本人在 MSDN 看到的關(guān)于 SetTimer 函數(shù)的一點(diǎn)總結(jié)了。本人限于能力,上文中難免有錯誤的地方,若讀者發(fā)現(xiàn)上文的錯誤,請于評論區(qū)中指出,本人看到之后會立即修改的,謝謝。
總結(jié)
以上是生活随笔為你收集整理的关于SetTimer函数的总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 改进的自适应中值滤波算法 去除椒盐噪声
- 下一篇: java动漫网站_基于SSM框架下的JA