Explains how to use timers to suspend a thread for a given time period.
Create a CTimer object.
Add the CTimer object to the current active
scheduler using CActiveScheduler::Add().
CTimer does not add itself to the active scheduler.
Start the timer
to complete after a set period by calling CTimer::After(), passing the required period in microseconds.
The CTimer::After() method inreturn calls CActive::SetActive() to indicate that the active object has issued a request.
The following code snippet uses CTimer, which is an active object, to generate events.
class CDelayUpdate : public CTimer { public:
/**
* static factory "constructor" function
*/
static CDelayUpdate* NewL();
/**
* Destructor
*/
~CDelayUpdate();
public:
/**
* Called to start the delayed update
*/
void startUpdate();
/**
* Called to stop further updates
*/
void stopUpdate();
public: //From CActive
/**
* Implements cancellation of an outstanding request.
*/
virtual void DoCancel();
/**
Handles an active object's request completion event.
*/
void RunL();
private:
/**
* Default constructor
*/
CDelayUpdate();
/**
* Second phase constructor
*/
void ConstructL();
private: // Data
TInt iCounter; // count a few loops
};
// CDelayUpdate* CDelayUpdate::NewL() {
CDelayUpdate* self = new (ELeave) CDelayUpdate();
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
CDelayUpdate::CDelayUpdate()
: CTimer(CActive::EPriorityStandard)
{
iCounter = 0;
CActiveScheduler::Add(this);
}
void CDelayUpdate::ConstructL() {
CTimer::ConstructL();
}
CDelayUpdate::~CDelayUpdate() {
Cancel();
//delete any objects owned by this class
}
void CDelayUpdate::startUpdate() {
if (IsActive()) {
// probably a programming error so could panic instead
return;
}
//perform anything before timer starts
After ( TTimeIntervalMicroSeconds32 (4000000) ); // 4 seconds
}
void CDelayUpdate::stopUpdate() {
if (IsActive()) {
Cancel();
}
else {
//probably a programming error so could panic
}
}
void CDelayUpdate::RunL() {
//RDebug::Print(_L("CDelayUpdate:runL"));
if (iCounter < 5) {
//do something
After ( 1000000 ); // 1 second
iCounter++;
}
}