三个timer相关的类之一 - CIdle

 

class CIdle : public CActive;

Description:

- An active object that performs low-priority processing when no higher-priority active objects are ready to run. (一个低优先级的活动对象,在没有其他的高优先级的活动对象准备运行的时候才会调用这个活动对象)

 

- An idle time active object together with its associated callback function may be used to implement potentially long running background tasks, such as spreadsheet recalculation and word processor repagination.

Construction and destruction:

static IMPORT_C CIdle *NewL(TInt aPriority);

- Allocates and initialises an Idle time active object, adds it to the active scheduler, but leaves on failure.

 

protected: IMPORT_C CIdle(TInt aPriority);

- Protected constructor taking a priority value. Sets this active object's priority value.

Member functions:

 

IMPORT_C void Start(TCallBack aCallBack);

- Starts the background task.

- The background task is encapsulated in the callback. The function represented by this callback is called every time this Idle time active object is scheduled to run.(仍然有个callback的函数会被调用,CIdle实现了CActiveRunLDoCancel,我们只需要构建一个CIdle的对象,然后调用其Start方法,实现我们的Callback函数即可。)

- The callback function should be structured to perform a background task in many increments, i.e. it should voluntarily relinquish control (i.e. return) after a suitable time interval to allow other, higher priority events to be handled. (用于处理后台任务,任务比较大,但是优先级不高)

- If the callback function has further work to do, it should return a true value. This ensures that the active object is scheduled to run again later. (如果还有事情要做就返回ETrue,如果任务都完成了就返回EFalse)

- Once the callback function has finally completed its work, it should return a false value. The active object is then no longer scheduled to run.

protected: virtual IMPORT_C void RunL();

- Handles this idle active object's request completion event. It is called when nothing of a higher priority can be scheduled.

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 IdleCallBack(TAny* aAny);

 

    /**

    * Notice that this is a sample fuction.

    */

    void SomeFunction();

 

private:    // Member data

    /**

    * The periodic timer. Owned by CMyClass

    */

    CIdle* iIdleTimer;

};

 

CMyClass::~CMyClass()

{

    if (iIdleTimer)

    {

       // Calling Cancel without checking if the timer is active is safe

       iIdleTimer ->Cancel();

    }

    delete iIdleTimer;

}

void CMyClass::ConstructL()

{

    if (!iIdleTimer)

    {

    iIdleTimer = CIdle::NewL( CActive::EPriorityIdle );

    }

    if (iIdleTimer ->IsActive() )

    {

       iIdleTimer ->Cancel();

    }

    iIdleTimer ->Start( TCallBack(PeriodicTimerCallBack, this ) );

}

TInt CMyClass::PeriodicTimerCallBack(TAny* aAny)

{

    CMyClass* self = static_cast<CMyClass*>( aAny );

    return self->SomeFunction();

}

void CMyClass::SomeFunction()

{

    if (finished)

{

    return EFalse;

}

else

    {

       return ETrue;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值