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. }     
相關文章
相關標籤/搜索