轉載的地址:ios
http://blog.163.com/linzuxin@126/blog/static/340740572008101311552948/windows
1.1 用WM_TIMER來設置定時器
先請看SetTimer這個API函數的原型
app
UINT_PTR SetTimer(
- HWND hWnd,
- UINT_PTR nIDEvent,
- UINT uElapse,
- TIMERPROC lpTimerFunc
- );
例如
SetTimer(m_hWnd,1,1000,NULL); //一個1秒觸發一次的定時器
在MFC程序中SetTimer被封裝在CWnd類中,調用就不用指定窗口句柄了,例如:
UINT SetTimer(1,100,NULL);
函數反回值就是第一個參數值1,表示此定時器的ID號。
第二個參數表示要等待100毫秒時間再從新處理一次。第三個參數在這種方法中通常用NULL。
注意:設置第二個參數時要注意,若是設置的等待時間比處理時間短,程序就會出問題了。
1.2 調用回調函數
此方法首先寫一個以下格式的回調函數
void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime);
而後再用SetTimer(1,100,TimerProc)函數來建一個定時器,第三個參數就是回調函數地址。
二、多個定時器的實現與應用
咱們在安裝定時器時都爲其指定了ID,使用多個定時器時,該ID就發揮做用了。
不使用MFC時,當接收到WM_TIMER消息,WPARAM wParam中的值即是該定時器的ID
使用MFC時就更簡單了,咱們爲其增長WM_TIME的消息處理函數OnTimer便可,請看以下例子
void CTimerTestDlg::OnTimer(UINT nIDEvent)
- {
- switch (nIDEvent)
- {
- case 24:
- Draw1();
- break;
- case 25:
- Draw2();
- break;
- }
- CDialog::OnTimer(nIDEvent);
- }
當你用回調函數時,咱們能夠根據nTimerid的值來判斷是哪一個定時器,例如:
void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
- {
- switch(nTimerid)
- {
- case 1:
- Do1();
- break;
- case 2:
- Do2();
- break;
- }
- }
三、取消定時器
再也不使用定時器後,咱們應該調用KillTimer來取消定時,KillTimer的原型以下
BOOL KillTimer(
- HWND hWnd,
- UINT_PTR uIDEvent
- );
在MFC程序中咱們能夠直接調用KillTimer(int nIDEvent)來取消定時器。
例子
#include <windows.h>
- #include <iostream>
- VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
- VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
- {
- std::cout < < "hello " < < std::endl;
- }
-
- void main()
- {
- int timer1 = 1;
- HWND hwndTimer;
- MSG msg;
-
- SetTimer(NULL,timer1,5000,TimerProc);
- int itemp;
- while ( (itemp = GetMessage(&msg, NULL,NULL,NULL))&& (itemp!=0) && (-1 != itemp))
- {
- if (msg.message == WM_TIMER)
- {
- std::cout < < "i got the message " < < std::endl;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- }
輸出以下:
i got the message
hello
i got the message
hello
i got the message
hello
---------------------------------------------------------------------------------------------------------------------------
-
-
-
-
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include <conio.h>
-
- unsigned long WINAPI Thread(PVOID pvoid);
- void main()
- {
- DWORD dwThreadId;
- printf("use timer in workthread of console application<masterz>\n");
- HANDLE hThread = CreateThread(
- NULL,
- 0,
- Thread,
- 0,
- 0,
- &dwThreadId);
- DWORD dwwait=WaitForSingleObject(hThread,1000*30);
- switch(dwwait)
- {
- case WAIT_ABANDONED:
- printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
- break;
- case WAIT_OBJECT_0:
- printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
- break;
- case WAIT_TIMEOUT:
- printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
- break;
- }
- CloseHandle(hThread);
- _getch();
- }
-
- unsigned long WINAPI Thread(PVOID pvoid)
- {
- MSG msg;
- PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
- UINT timerid=SetTimer(NULL,111,3000,NULL);
- BOOL bRet;
- int count =0;
- while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
- {
- if (bRet == -1)
- {
-
- }
- else
- if(msg.message==WM_TIMER)
- {
- count++;
- printf("WM_TIMER in work thread count=%d\n",count);
- if(count>4)
- break;
- }
- else
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- KillTimer(NULL,timerid);
- printf("thread end here\n");
- return 0;
- }