Symbian可用定时器种类: CTimer,RTimer,CPeriodic,CHearBeat
1.CPeriodic用法:因其简易性,最常被使用,需要实现回调函数。
- class CMyTimer : public CBase
- {
- public:
- CMyTimer();
- void StartTimer();
- void CancelTimer();
- static TInt Loop(TAny* aPtr);
- private:
- CPeriodic* iTimer;
- }
-
- CMyTimer::CMyTimer()
- {
- iTimer=CPeriodic::NewL(CActive::EPriorityStandard);
- StartTimer();
- }
-
- void CMyTimer::StartTimer()
- {
- iTimer->Start(500000,500000,TCallBack(Loop,this));
- }
-
- void CMyTimer::CancelTimer()
- {
- iTimer->Cancel();
- }
-
- TInt CMyTimer::Loop(TAny* aPtr)
- {
-
- }
2.RTimer用法:需要配合CActive对象进行使用
- class CMyTimer : public CActive
- {
- public:
- CMyTimer();
- private:
- void RunL();
- void DoCancel();
- void StartTimer();
- private:
- RTimer iTimer;
- }
-
- CMyTimer::CMyTimer()
- :CActive(EPriorityStandard)
- {
- iTimer.CreateLocal();
- CActiveScheduler::Add(this);
- StartTimer();
- }
-
- void CMyTimer::RunL()
- {
- if(iStatus.Int()==KErrNone)
- {
-
- StartTimer();
- }
- }
-
- void CMyTimer::DoCancel()
- {
- iTimer.Cancel();
- }
-
- void CMyTimer::StartTimer()
- {
- if(IsActive())return;
- iTimer.After(iStatus,500000);
- SetActive();
- }
3.CTimer用法:CTimer需要被继承使用,CTimer封装了对RTimer的使用
- class CMyTimer : public CTimer
- {
- public:
- static CMyTimer* NewLC();
- static CMyTimer* NewL();
- private:
- CMyTimer():CTimer(EPriorityStandard){}
- void ConstructL();
- void RunL();
- }
-
- CMyTimer* CMyTimer::NewLC()
- {
- CMyTimer* self=new(ELeave) CMyTimer();
- CleanupStack::PushL(self);
- self->ConstructL();
- return self;
- }
-
- CMyTimer* CMyTimer::NewL()
- {
- CMyTImer* self=CMyTimer::NewLC();
- CleanupStack(self);
- return self;
- }
-
- void CMyTimer::ConstructL()
- {
- CTimer::ConstructL();
- CActiveScheduler::Add(this);
- After(5000000);
- }
-
- void CMyTimer::RunL()
- {
-
- After(5000000);
- }
阅读(548) | 评论(0) | 转发(0) |