Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3345028
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: C/C++

2009-07-22 18:11:28

Symbian可用定时器种类: CTimer,RTimer,CPeriodic,CHearBeat

1.CPeriodic用法:因其简易性,最常被使用,需要实现回调函数。

  1. class CMyTimer : public CBase  
  2. {  
  3. public:  
  4.     CMyTimer();  
  5.     void StartTimer();  
  6.     void CancelTimer();  
  7.     static TInt Loop(TAny* aPtr);//回调函数  
  8. private:  
  9.     CPeriodic* iTimer;   
  10. }  
  11.   
  12. CMyTimer::CMyTimer()  
  13. {  
  14.     iTimer=CPeriodic::NewL(CActive::EPriorityStandard);  
  15.     StartTimer();  
  16. }  
  17.   
  18. void CMyTimer::StartTimer()  
  19. {  
  20.     iTimer->Start(500000,500000,TCallBack(Loop,this));//请求只需发送一次  
  21. }  
  22.   
  23. void CMyTimer::CancelTimer()  
  24. {  
  25.     iTimer->Cancel();  
  26. }  
  27.   
  28. TInt CMyTimer::Loop(TAny* aPtr)  
  29. {  
  30.     //循环体,可以在此加入代码  
  31. }  

2.RTimer用法:需要配合CActive对象进行使用

  1. class CMyTimer : public CActive  
  2. {  
  3. public:  
  4.     CMyTimer();  
  5. private:  
  6.     void RunL();  
  7.     void DoCancel();  
  8.     void StartTimer();  
  9. private:  
  10.     RTimer iTimer;  
  11. }  
  12.   
  13. CMyTimer::CMyTimer()  
  14. :CActive(EPriorityStandard)  
  15. {  
  16.     iTimer.CreateLocal();  
  17.     CActiveScheduler::Add(this);  
  18.     StartTimer();  
  19. }  
  20.   
  21. void CMyTimer::RunL()  
  22. {  
  23.     if(iStatus.Int()==KErrNone)//iStatus为CActive的成员变量,切勿自己定义  
  24.     {  
  25.     //循环体,可以在此加入代码  
  26.     StartTimer();  
  27.     }  
  28. }  
  29.   
  30. void CMyTimer::DoCancel()  
  31. {  
  32.     iTimer.Cancel();  
  33. }  
  34.   
  35. void CMyTimer::StartTimer()  
  36. {  
  37.     if(IsActive())return;  
  38.     iTimer.After(iStatus,500000);  
  39.     SetActive();  
  40. }  

3.CTimer用法:CTimer需要被继承使用,CTimer封装了对RTimer的使用

  1. class CMyTimer : public CTimer  
  2. {  
  3. public:  
  4.     static CMyTimer* NewLC();  
  5.     static CMyTimer* NewL();  
  6. private:  
  7.     CMyTimer():CTimer(EPriorityStandard){}  
  8.     void ConstructL();  
  9.     void RunL();  
  10. }  
  11.   
  12. CMyTimer* CMyTimer::NewLC()  
  13. {  
  14.     CMyTimer* self=new(ELeave) CMyTimer();  
  15.     CleanupStack::PushL(self);  
  16.     self->ConstructL();  
  17.     return self;  
  18. }  
  19.   
  20. CMyTimer* CMyTimer::NewL()  
  21. {  
  22.     CMyTImer* self=CMyTimer::NewLC();  
  23.     CleanupStack(self);  
  24.     return self;  
  25. }  
  26.   
  27. void CMyTimer::ConstructL()  
  28. {  
  29.     CTimer::ConstructL();  
  30.     CActiveScheduler::Add(this);  
  31.     After(5000000);  
  32. }  
  33.   
  34. void CMyTimer::RunL()  
  35. {  
  36.     //循环体,可以在此加入代码  
  37.     After(5000000);  
  38. }  

阅读(486) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~