C++ 中定时器的用法

C++ 中定时器的用法

转载的地址:ios

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


1.1   用WM_TIMER来设置定时器 

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

app

UINT_PTR   SetTimer(   
  1. HWND   hWnd,   //   窗口句柄    
  2. UINT_PTR   nIDEvent,   //   定时器ID,多个定时器时,能够经过该ID判断是哪一个定时器    
  3. UINT   uElapse,   //   时间间隔,单位为毫秒    
  4. TIMERPROC   lpTimerFunc   //   回调函数    
  5. );   



例如 
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)   
  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. }   


当你用回调函数时,咱们能够根据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. }   


三、取消定时器 

再也不使用定时器后,咱们应该调用KillTimer来取消定时,KillTimer的原型以下 

BOOL   KillTimer(   
  1. HWND   hWnd,   //   窗口句柄    
  2. UINT_PTR   uIDEvent   //   ID    
  3. );   


在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. }   



输出以下: 
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. }     
相关文章
相关标签/搜索