Mini timer

Anyone who has used a timer knows that you generally need to inherit a class and add a callback. Sometimes you need to use multiple timers to call back a thing. In short, it is more troublesome to use.

I simplified it a bit. hope its good for U.S..

  1. /**
  2. * author :windcao [email protected]
  3. * file: minitimer.h
  4. * desc: a mini and useful timer
  5. * copyright: windcao ,2004-08-02
  6. */
  7. #ifndef _WINDCAO_MINI_TIMER_
  8. #define _WINDCAO_MINI_TIMER_
  9. class MMiniTimerObserver
  10. {
  11. public:
  12.     virtual void OnTimeOut(TInt id)=0;
  13. };
  14. class CMiniTimer:public CTimer
  15. {
  16. public
  17.     CMiniTimer(TInt aPriority,MMiniTimerObserver& aObserver,TInt aId=0)
  18.         : CTimer(aPriority),iObserver(aObserver),iID(aId){  CActiveScheduler::Add(this);}
  19.     virtual ~CMiniTimer(){  Cancel();}
  20. private:
  21.     void RunL(){ if(iStatus==KErrNone)iObserver.OnTimeOut(this->iID);}   
  22.     //don`t use this timer to at
  23.     void At(const TTime& aTime){}
  24.     MMiniTimerObserver & iObserver;
  25.     TInt iID;
  26. };
  27. #endif _WINDCAO_MINI_TIMER_

 

 

Instructions:

1 Add the header file #include "minitimer.h"

2 To implement MMiniTimerObserver, if multiple timers use one observer, you need to define multiple ids

3 construct timer

  1. CMiniTimer iTimer1;
  2. CMiniTimer iTimer2;
  3. CMiniTimer iTimer3;
  4. iTimer1= CMiniTimer(CActive::EPrioprtyIdle,aObserver,EFirst);
  5. iTimer2 = CMiniTimer (CActive :: EPrioprtyIdle, Observer, Second);
  6. iTimer3= CMiniTimer(CActive::EPrioprtyIdle,aObserver,EThrid);

4 Use CTimer::After to start the timer. Note that this timer is not recommended for At operation. Generally there will be problems.

Guess you like

Origin blog.csdn.net/windcao/article/details/2760917