CExampleTimer implementation shown in CExampleTimer.cpp illustrates simple timer implementation using active objects and RTimer. To construct the time just use the static NewL function and supply the active object priority along side with the callback interface to be notified when the timer expires.
The priority value should be one of the priorities defined inside CActive (i.e EPriorityIdle, EPriorityLow, EPriorityStandard, EPriorityUserInput or EPriorityHigh)
And for activing the timer there are three public function provided, which are:
1. At(aTime), timer is set to expire at the time specified.
2. After(aInterval), timer is set to expire after the specified interval.
3. Inactivity(aSeconds), timer is set to expire after specified interval of user inactivity.
CExampleTimer::CExampleTimer(const TInt aPriority,MExampleTimerNotify& aNotify) :CActive(aPriority),iNotify(aNotify) { } CExampleTimer::~CExampleTimer() { Cancel(); iTimer.Close(); } CExampleTimer* CExampleTimer::NewL(const TInt aPriority,MExampleTimerNotify& aNotify) { CExampleTimer* me = new (ELeave) CExampleTimer(aPriority,aNotify); CleanupStack::PushL(me); me->ConstructL(); CleanupStack::Pop(); return me; } void CExampleTimer::ConstructL(void) { CActiveScheduler::Add(this); iTimer.CreateLocal(); } void CExampleTimer::After(TTimeIntervalMicroSeconds32 aInterval) { Cancel(); iTimer.After(iStatus,aInterval); SetActive(); } void CExampleTimer::At(const TTime& aTime) { Cancel(); iTimer.At(iStatus,aTime); SetActive(); } void CExampleTimer::Inactivity(TTimeIntervalSeconds aSeconds) { Cancel(); iTimer.Inactivity(iStatus,aSeconds); SetActive(); } void CExampleTimer::DoCancel() { iTimer.Cancel(); } void CExampleTimer::RunL() { iNotify.TimerExpired(this,iStatus.Int()); }
#include <E32BASE.H> class MExampleTimerNotify { public: virtual void TimerExpired(TAny* aTimer,TInt aError) = 0; }; class CExampleTimer: public CActive { public: static CExampleTimer* NewL(const TInt aPriority,MExampleTimerNotify& aNotify); ~CExampleTimer(); public: void At(const TTime& aTime); void After(TTimeIntervalMicroSeconds32 aInterval); void Inactivity(TTimeIntervalSeconds aSeconds); protected: void RunL(); void DoCancel(); private: CExampleTimer(const TInt aPriority,MExampleTimerNotify& aNotify); void ConstructL(void); private: RTimer iTimer; MExampleTimerNotify& iNotify; };
The basic .h file for the timer controller class, demonstrating how to use the timer
class CYourTimerController : public CBase, public MExampleTimerNotify { public: .... void TimerExpired(TAny* aTimer,TInt aError); private : CExampleTimer* iYourTimer; };
The basic .cpp file for the controller class
void CYourTimerController::ConstructL() { iYourTimer = CExampleTimer::NewL(CActive::EPriorityStandard, *this); TTimeIntervalMicroSeconds32 someInterVal(200000); //you can call After/At/Inactivity depending on what you want to do iYourTimer->After(someInterVal); } /** * Callback implementation when the timer activity happens in the CExampleTimer class **/ void CYourTimerController::TimerExpired(TAny* aTimer,TInt aError) { if(aError == KErrNone) { // Timer successfully completed, handle it CExampleTimer* timer = (CExampleTimer*)aTimer; TTimeIntervalSeconds seconds(10); timer->Inactivity(seconds); //Notify inactivity after 10 seconds } } The NewL/NewLC and other functions have been omitted as the implementation details would vary from case to case.
Note : When the system time changes, the At-timers will complete immediately with the result KErrAbort. So this must be handled by the applicaiton.