C++定時器的使用
原文鏈接:http://blog.163.com/linzuxin@126/blog/static/340740572008101311552948/
1.1 用WM_TIMER來設置定時器
先請看SetTimer這個API函數的原型
UINT_PTR SetTimer(
HWND hWnd, // 窗口句柄
UINT_PTR nIDEvent, // 定時器ID,多個定時器時,可以通過該ID判斷是哪個定時器
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)函數來建一個定時器,第三個參數就是回調函數地址。
2、多個定時器的實現與應用
我們在安裝定時器時都爲其指定了ID,使用多個定時器時,該ID就發揮作用了。
不使用MFC時,當接收到WM_TIMER消息,WPARAM wParam中的值便是該定時器的ID
使用MFC時就更簡單了,我們爲其增加WM_TIME的消息處理函數OnTimer即可,請看如下例子
void CTimerTestDlg::OnTimer(UINT nIDEvent)
{
switch (nIDEvent)
{
case 24: ///處理ID爲24的定時器
Draw1();
break;
case 25: ///處理ID爲25的定時器
Draw2();
break;
}
CDialog::OnTimer(nIDEvent);
}
當你用回調函數時,我們可以根據nTimerid的值來判斷是哪個定時器,例如:
void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
{
switch(nTimerid)
{
case 1: ///處理ID爲1的定時器
Do1();
break;
case 2: ///處理ID爲2的定時器
Do2();
break;
}
}
3、取消定時器
不再使用定時器後,我們應該調用KillTimer來取消定時,KillTimer的原型如下
BOOL KillTimer(
HWND hWnd, // 窗口句柄
UINT_PTR uIDEvent // ID
);
在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
---------------------------------------------------------------------------------------------------------------------------
// timer.cpp : 定義控制檯應用程序的入口點。
//
#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, // no security attributes
0, // use default stack size
Thread, // thread function
0, // argument to thread function
0, // use default creation flags
&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)
{
// handle the error and possibly exit
}
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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.