You Are Here:

Community: Wiki

This page was last modified on 30 September 2009, at 11:44.

Simple Timer implementation

From Forum Nokia Wiki

Reviewer Approved   

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;
};

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.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fE25E454E25B8E25AE44E25E456E2596E2587E5fS60X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx2E2E2500X