C++ 中定时器的用法

C++ 中定时器的用法

转载的地址:

http://blog.163.com/linzuxin@126/blog/static/340740572008101311552948/


1.1   用WM_TIMER来设置定时器 

先请看SetTimer这个API函数的原型 

UINT_PTR   SetTimer(   
  1. HWND   hWnd,   //   窗口句柄    
  2. UINT_PTR   nIDEvent,   //   定时器ID,多个定时器时,可以通过该ID判断是哪个定时器    
  3. UINT   uElapse,   //   时间间隔,单位为毫秒    
  4. TIMERPROC   lpTimerFunc   //   回调函数    
  5. );   
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)   
  1. {   
  2. switch   (nIDEvent)   
  3. {   
  4. case   24:   ///处理ID为24的定时器    
  5. Draw1();   
  6. break;   
  7. case   25:   ///处理ID为25的定时器    
  8. Draw2();   
  9. break;   
  10. }   
  11. CDialog::OnTimer(nIDEvent);   
  12. }   
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)   
  1. {   
  2. switch(nTimerid)   
  3. {   
  4. case   1:   ///处理ID为1的定时器    
  5. Do1();   
  6. break;   
  7. case   2:   ///处理ID为2的定时器    
  8. Do2();   
  9. break;   
  10. }   
  11. }   
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(   
  1. HWND   hWnd,   //   窗口句柄    
  2. UINT_PTR   uIDEvent   //   ID    
  3. );   
BOOL KillTimer( HWND hWnd, // 窗口句柄 UINT_PTR uIDEvent // ID );

在MFC程序中我们可以直接调用KillTimer(int   nIDEvent)来取消定时器。
例子
#include   <windows.h>    
  1. #include   <iostream>    
  2. VOID   CALLBACK   TimerProc(HWND   hwnd,UINT   uMsg,UINT   idEvent,DWORD   dwTime);   
  3. VOID   CALLBACK   TimerProc(HWND   hwnd,UINT   uMsg,UINT   idEvent,DWORD   dwTime)   
  4. {   
  5. std::cout   < <   "hello "   < <   std::endl;   
  6. }   
  7.   
  8. void   main()   
  9. {   
  10. int   timer1   =   1;   
  11. HWND   hwndTimer;         
  12. MSG   msg;                       
  13.   
  14. SetTimer(NULL,timer1,5000,TimerProc);   
  15. int   itemp;   
  16. while ( (itemp = GetMessage(&msg, NULL,NULL,NULL))&& (itemp!=0) &&  (-1 !=  itemp))   
  17. {     
  18.    if   (msg.message   ==   WM_TIMER)     
  19.    {     
  20.     std::cout   < <   "i   got   the   message "   < <   std::endl;   
  21.     TranslateMessage(&msg);     
  22.     DispatchMessage(&msg);       
  23.     }     
  24. }     
  25. }   
#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

---------------------------------------------------------------------------------------------------------------------------  
  1.   
  2. // timer.cpp : 定义控制台应用程序的入口点。   
  3. //   
  4.   
  5. #include "stdafx.h"   
  6. #include   <windows.h>     
  7. #include   <stdio.h>     
  8. #include   <conio.h>     
  9.   
  10. unsigned   long   WINAPI   Thread(PVOID   pvoid);    
  11. void   main()    
  12. {    
  13.     DWORD   dwThreadId;    
  14.     printf("use   timer   in   workthread   of   console   application<masterz>\n");    
  15.     HANDLE   hThread   =   CreateThread(      
  16.         NULL,                                                 //   no   security   attributes       
  17.         0,                                                       //   use   default   stack   size         
  18.         Thread,                                     //   thread   function       
  19.         0,                                 //   argument   to   thread   function       
  20.         0,                                                       //   use   default   creation   flags       
  21.         &dwThreadId);      
  22.     DWORD   dwwait=WaitForSingleObject(hThread,1000*30);    
  23.     switch(dwwait)    
  24.     {    
  25.     case   WAIT_ABANDONED:    
  26.         printf("main   thread   WaitForSingleObject   return   WAIT_ABANDONED\n");    
  27.         break;    
  28.     case   WAIT_OBJECT_0:    
  29.         printf("main   thread   WaitForSingleObject   return   WAIT_OBJECT_0\n");    
  30.         break;    
  31.     case   WAIT_TIMEOUT:    
  32.         printf("main   thread   WaitForSingleObject   return   WAIT_TIMEOUT\n");    
  33.         break;    
  34.     }    
  35.     CloseHandle(hThread);    
  36.     _getch();    
  37. }    
  38.   
  39. unsigned   long   WINAPI   Thread(PVOID   pvoid)    
  40. {    
  41.     MSG   msg;    
  42.     PeekMessage(&msg,   NULL,   WM_USER,   WM_USER,   PM_NOREMOVE);    
  43.     UINT   timerid=SetTimer(NULL,111,3000,NULL);    
  44.     BOOL   bRet;    
  45.     int   count   =0;    
  46.     while(   (bRet   =   GetMessage(   &msg,   NULL,   0,   0   ))   !=   0)    
  47.     {      
  48.         if   (bRet   ==   -1)    
  49.         {    
  50.             //   handle   the   error   and   possibly   exit     
  51.         }    
  52.         else    
  53.             if(msg.message==WM_TIMER)    
  54.             {    
  55.                 count++;    
  56.                 printf("WM_TIMER   in   work   thread   count=%d\n",count);    
  57.                 if(count>4)    
  58.                     break;    
  59.             }    
  60.             else    
  61.             {    
  62.                 TranslateMessage(&msg);      
  63.                 DispatchMessage(&msg);      
  64.             }    
  65.     }    
  66.     KillTimer(NULL,timerid);    
  67.     printf("thread   end   here\n");    
  68.     return   0;    
  69. }     

你可能感兴趣的:(定时器,的)