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..
- /**
- * author :windcao [email protected]
- * file: minitimer.h
- * desc: a mini and useful timer
- * copyright: windcao ,2004-08-02
- */
- #ifndef _WINDCAO_MINI_TIMER_
- #define _WINDCAO_MINI_TIMER_
- class MMiniTimerObserver
- {
- public:
- virtual void OnTimeOut(TInt id)=0;
- };
- class CMiniTimer:public CTimer
- {
- public:
- CMiniTimer(TInt aPriority,MMiniTimerObserver& aObserver,TInt aId=0)
- : CTimer(aPriority),iObserver(aObserver),iID(aId){ CActiveScheduler::Add(this);}
- virtual ~CMiniTimer(){ Cancel();}
- private:
- void RunL(){ if(iStatus==KErrNone)iObserver.OnTimeOut(this->iID);}
- //don`t use this timer to at
- void At(const TTime& aTime){}
- MMiniTimerObserver & iObserver;
- TInt iID;
- };
- #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
- CMiniTimer iTimer1;
- CMiniTimer iTimer2;
- CMiniTimer iTimer3;
- iTimer1= CMiniTimer(CActive::EPrioprtyIdle,aObserver,EFirst);
- iTimer2 = CMiniTimer (CActive :: EPrioprtyIdle, Observer, Second);
- 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.