class CPeriodic : public CTimer;
Description:
- Periodic timer active object.
- This class generates regular timer events and handles them with a callback function. The callback is specified as a parameter to Start().(周期性的产生定时时间)
- The callback may not be called immediately after the signal from the timer request has been generated, for the following reasons:
1. the RunL() of another active object may be running at the time of the signal
2. other active objects may have a higher priority than the CPeriodic
- If timing accuracy is important to your application, you can minimise the first problem by ensuring all RunL()s complete quickly, and can eliminate the second by giving the CPeriodic a higher priority than any other active object. Although it is generally recommended that timer-related active objects have a high priority, this will not address the problem of CPeriodic timers running behind, because active object scheduling is not pre-emptive. (如果需要精确定时的话,需要提高CPeriodic优先级或是使得RunL()尽可能的短)
- After a timer signal generated by a CPeriodic, the next signal is requested just before running the callback, and this request can be delayed for the same reasons that running the callback can be delayed. Therefore, a large number N of periods may add up to somewhat more than N times the requested period time. If absolute precision is required in tracking time, do not rely on counting the number of times the callback is called: read the value of the system clock every time you need it.
Construction and destruction:
static IMPORT_C CPeriodic *NewL(TInt aPriority);
- Allocates and constructs a CPeriodic object - leaving.
- Specify a high priority so the callback function is scheduled as soon as possible after the timer events complete.
protected: IMPORT_C CPeriodic(TInt aPriority);
- Classes derived from CPeriodic must define and provide a constructor through which the priority of the active object can be passed. Such a constructor can call CPeriodic's constructor in its constructor initialisation list.
Member functions:
void Start(TTimeIntervalMicroSeconds32 aDelay, TTimeIntervalMicroSeconds32 anInterval, TCallBack aCallBack);
- Starts generating periodic events.
- The event calls the protected RunL() function, which in turn calls the function specified by aCallBack. The first event is generated after aDelay microseconds; subsequent events are generated regularly thereafter at intervals of anInterval microseconds.( 时间到达后,AS会调用RunL(),其又会调用这个callback函数)
- The TCallBack contains a function pointer and a TAny* pointer. The function will be repeatedly called with the pointer as a parameter.
- Once started, periodic events are generated until the CPeriodic object is destroyed.
protected: virtual IMPORT_C void RunL();
- Handles an active object's request completion event.
- A derived class must provide an implementation to handle the completed request. If appropriate, it may issue another request.
- The function is called by the active scheduler when a request completion event occurs, i.e. after the active scheduler's WaitForAnyRequest() function completes.
Example:
// IMPLEMENTATION SPECIFIC CONSTANTS
const TInt KPeriodicTimerInterval5Sec(5000000);
class CMyClass : public CBase
{
....
private: // New functions
/**
* The call back function.
* /param aAny A pointer to this class.
*/
static TInt PeriodicTimerCallBack(TAny* aAny);
/**
* Notice that this is a sample fuction.
*/
void SomeFunction();
private: // Member data
/**
* The periodic timer.
* Owned by CMyClass
*/
CPeriodic* iPeriodicTimer;
};
CMyClass::~CMyClass()
{
if (iPeriodicTimer)
{
// Calling Cancel without checking if the timer is active is safe
iPeriodicTimer->Cancel();
}
delete iPeriodicTimer;
}
void CMyClass::ConstructL()
{
// Initialize the periodic timer.
iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityIdle);
// Start the periodic timer, when ever the time elapsed
// the PeriodicTimerCallBack() will get called.
// Notice: The timer will periodically hit the PeriodicTimerCallBack
// until you cancel the timer by calling iPeriodicTimer->Cancel().
iPeriodicTimer->Start(KPeriodicTimerInterval5Sec, KPeriodicTimerInterval5Sec, TCallBack(PeriodicTimerCallBack, this));
}
TInt CMyClass::PeriodicTimerCallBack(TAny* aAny)
{
CMyClass* self = static_cast<CMyClass*>( aAny );
// TODO: The below call is a sample function,
// you may change it to your requirement.
self->SomeFunction();
// Cancel the timer when the callback should not be called again.
// Call: self->iPeriodicTimer->Cancel();
return KErrNone; // Return value ignored by CPeriodic
}
void CMyClass::SomeFunction()
{
// TODO: Your code!
}
CPeriodic 继承了CTimer所以做了一些必要的工作,比如调用基类的默认构造函数来设置优先级,调用AS的add方法等等。