You Are Here:

Community: Wiki

This page was last modified 18:41, 3 July 2009.

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

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.

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditFurlTechnocratiMagnoliaTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fMMPE5ffileX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxMMPE20fileE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfnTypeZCommunityContentQ qdcZtypeQUqfnTypeZE52esourceQ qdcZtypeQUqfnTypeZWebpageQ qdcZtypeQUqfnTypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfnTypeZCommunityContentQ qfnZtypeQUqfnTypeZE52esourceQ qfnZtypeQUqfnTypeZWebpageQ qfnZtypeQUqfnTypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxfileX qfnZuserE5ftagQSxlibpathX qfnZuserE5ftagQSxmmpX qfnZuserE5ftagQSxresourceX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfnTypeZCommunityContentQ qrdfZtypeQUqfnTypeZE52esourceQ qrdfZtypeQUqfnTypeZWebpageQ qrdfZtypeQUqfnTypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx2E2E2500X