Simple Timer implementation
Article Metadata
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 callback interface to be notified when the timer expires.
And for activating 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(MExampleTimerNotify& aNotify)
:CActive(EPriorityStandard),iNotify(aNotify)
{
}
CExampleTimer::~CExampleTimer()
{
Cancel();
iTimer.Close();
}
CExampleTimer* CExampleTimer::NewL(MExampleTimerNotify& aNotify)
{
CExampleTimer* me = new (ELeave) CExampleTimer(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(MExampleTimerNotify& aNotify);
~CExampleTimer();
public:
void At(const TTime& aTime);
void After(TTimeIntervalMicroSeconds32 aInterval);
void Inactivity(TTimeIntervalSeconds aSeconds);
protected:
void RunL();
void DoCancel();
private:
CExampleTimer(MExampleTimerNotify& aNotify);
void ConstructL();
private:
RTimer iTimer;
MExampleTimerNotify& iNotify;
};
The basic .h file for the timer controller class, demonstrating how to use the timer.
How to use this class ?
1) Inherit your application class from MExampleTimerNotify, your class can be any view or the AppUi class. 2) Add the TimerExpired method to your class as it has been declared pure virtual you need to implement that. 3) Declare the variable of CExampleTimer class in your class. 4) In example below it has been created in ConstructL 5) Call After() method with appropriate time and wait for the callback even i.e. TimerExpired.
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(*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 application. Also read about At() function in the SDK help which explains about KErrUnderflow and KErrOverFlow which you may get on completion of the timer request.


can you please brief the call procedure of timer from my application
How to invoke it i ve set the priority but the next parameter is doubt ful can you explain
Example is incomplete
There is no demonstration of how to instantiate as per previous comment. MExampleTimerNotify is poorly documented. How do you use it's event handler?
The example has been updated for its usage
The article contains the details of how to use it as per request of the user.
- vdharankar
Murugaprabum - Nice writeup
A well written code and explanation to understand RTimer and Active objects for symbian beginners. Very happy.murugaprabum 14:05, 16 April 2012 (EEST)