This page was last modified 17:17, 20 May 2008.
Simple Timer implementation
From Forum Nokia Wiki
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.cpp
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()); }
CExampleTimer.h
#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; };
Note : When the system time changes, the At-timers will complete immediately with the result KErrAbort. So this must be handled by the applicaiton.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sleep/Wait during dialogs | Pranoy | General Symbian C++ | 4 | 2007-03-28 11:40 |
| What is SO HARD About HAVING A CALL TIMER? | funkyblue | General Discussion | 1 | 2006-06-17 11:23 |
| ctimer problem | sanknokia | General Symbian C++ | 4 | 2004-08-25 16:54 |
| autostart an application | leuchovius | General Symbian C++ | 44 | 2008-04-21 00:55 |
| Handling incoming calls | anoopd | General Symbian C++ | 2 | 2007-09-24 08:48 |
