三个timer相关的类之一

news/2024/7/4 13:09:22

 - CTimer 收藏
class CTimer : public CActive;

 

Description:


- Base class for a timer active object.

- This is an active object that uses the asynchronous services provided by RTimer, to generate events. These events occur either at a specific time specified as a TTime, or after an interval specified in microseconds. (使用的两个API,At() or After())

- The RunL() virtual member function is called by the active scheduler after this event occurs.

- To write a class derived from CTimer, first define and implement a constructor through which the priority of the CTimer active object can be specified. Then define and implement a suitable RunL() function to handle the completion of a timer request. This function is not defined by CTimer itself and must, therefore, be provided by the derived class. (派生类在其默认构造函数的初始化列表里面调用CTimer的构造函数来初始化CTimer的优先级。而且RunL()在CTimer里没有做实现,所以派生类必须做实现,CTimer实现了DoCancel())

- Note that the CPeriodic and CHeartbeat classes are derived from CTimer, and answer most timing needs.

 

Construction and destruction:

 

protected: IMPORT_C CTimer(TInt aPriority);

- Classes derived from CTimer must define and provide a constructor through which the priority of the active object can be passed. Such a constructor can call CTimer's constructor in its constructor initialisation list.

 

protected: IMPORT_C void ConstructL();

- The function must be called before any timer requests (i.e. calls to RTimer::After() or RTimer::At()) can be made.

- Since it is protected, it cannot be called directly by clients of CTimer derived classes. Typically, a derived class makes a base call to this function in the second phase of two-phase construction; i.e. the derived class defines and implements its own ConstructL() function within which it makes a base call to CTimer::ConstructL().

(继续说明在派生类的默认构造函数的初始化列表里面初始化CTimer的优先级,和必须在ConstructL()里面调用CTimer的ConstructL())

 

Member functions:


IMPORT_C void At(const TTime &aTime);

- Requests an event at a given local time.

- Notes:

   1. The CTimer' RunL() function will be run as soon as possible after the specified system time.

   2. The RunL() may be delayed because the RunL() of another active object, with the deepest nesting-level active scheduler on the same thread, is running when the event occurs: this cannot be avoided, but can be minimised by making all RunL()s of short duration.

   3. The RunL() may be delayed because other, higher-priority, active objects are scheduled instead. This can be avoided by making CTimers very high-priority.

   4. The TTime object should be set to the home time.

IMPORT_C void After(TTimeIntervalMicroSeconds32 anInterval);

- Requests an event after an interval.

- This timer completes after the specified number of microseconds. The "after timer" counter stops during power-down. Therefore, a 5-second timer will complete late if the machine is turned off 2 seconds after the request is made.

protected: virtual IMPORT_C void DoCancel();

- Implements cancellation of an outstanding request

- This function is called as part of the active object's Cancel().

- It must call the appropriate cancel function offered by the active object's asynchronous service provider. The asynchronous service provider's cancel is expected to act immediately.
 


Example:


// 用CTimer实现一个定时器,结合Obsever模式,使其更common的使用

class MTimeOutTimer

{

public:

    virtual void TimeExpired() = 0;

};

 

class CTimeOutTimer : public CTimer

{

public:

    static CTimeOutTimer* NewL(MTimeOutTimer& aTimeOutTimer);

    static CTimeOutTimer* NewLC(MTimeOutTimer& aTimeOutTimer);

    ~CTimeOutTimer();  

protected:

    virtual void RunL();   

private:

    CTimeOutTimer(MTimeOutTimer& aTimeOutTimer);

    void ConstructL();

private:

    MTimeOutTimer& iTimer;

};

 

#include "TimeOutTimer.h"

 

CTimeOutTimer* CTimeOutTimer::NewL(MTimeOutTimer& aTimeOutTimer)

{

    CTimeOutTimer* self = CTimeOutTimer::NewLC(aTimeOutTimer);

    CleanupStack::Pop(self);

    return self;

}

 

CTimeOutTimer* CTimeOutTimer::NewLC(MTimeOutTimer& aTimeOutTimer)

{

    CTimeOutTimer* self = new (ELeave) CTimeOutTimer(aTimeOutTimer);

    CleanupStack::PushL(self);

    self->ConstructL();

    return self;

}

 

CTimeOutTimer::CTimeOutTimer(MTimeOutTimer& aTimeOutTimer)

    : CTimer(EPriorityStandard) //初始化优先级

    , iTimer(aTimeOutTimer)

{

}

 

CTimeOutTimer::~CTimeOutTimer()

{

    Cancel();

}

 

void CTimeOutTimer::ConstructL()

{

    CTimer::ConstructL(); // 调用基类的构造

    CActiveScheduler::Add(this);

}

 

void CTimeOutTimer::RunL()

{

    iTimer.TimeExpired(); // 和Observer模式相结合使用

}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jasonfqw/archive/2009/11/26/4875958.aspx


http://www.niftyadmin.cn/n/1990982.html

相关文章

Task19——相同的树

题目: 给定两个二叉树,编写一个函数来检验它们是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1,2,3]输出: true …

C/C++圣战 李维

C/C圣战 李维 Borland C/C的反击 当Microsoft Visual C 1.0 在C/C开发工具市场获得了空前成果的之后,Borland 才从Borland C/C 3.1的胜利梦中惊醒,思考如何面对Visual C的猛烈功势。事实上当时的Borland如果脑袋清醒一点,好好看清当时C/C开发…

深入浅析:面向对象编程四大原则

http://www.csai.cn 作者:佚名 来源:网络 2008年5月5日  进入社区   在面向对象设计中,如何通过很小的设计改变就可以应对设计需求的变化,这是令设计者极为关注的问题。为此不少OO先驱提出了很多有关面向对象的设计原则用于…

Task20——对称二叉树

题目: 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1/ \2 2/ \ / \ 3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1/ \2 2\ \3 3说明: 如果你可以运用递归和迭代两种方…

Task21——二叉树的最大深度

题目: 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 …

简单而复杂的smarty循环问题

简单而复杂的smarty循环问题 问:$mySmarty->assign(sCount, 20); $mySmarty->display(xxx.tpl); 我想要的很简单&#xff0c;就是能在xxx.tpl中循环$sCount变量&#xff0c;就像类似于下面的PHP循环形式&#xff1a; for ($i 0; $i < $sCount; $i) { ...... } 每个语…

系统API RFile::Seek的BUG

TInt RFile::Seek(TSeek aMode,TInt& aPos) const函数&#xff0c;当输入参数aPos超过文件范围时&#xff0c;函数不会返回错误值。我在CFileSource类里面实现了这样一个函数 TInt CFileSource::DoSeek(TInt aPos) { TInt err iRFile.Seek(ESeekStart, aPos); …