三个timer相关的类之一 - 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. (使用的两个APIAt() 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()里面调用CTimerConstructL())

 

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模式相结合使用

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值