简单实现一个延时或者计时功能

本文介绍了一个简单的周期定时器实现方法,包括创建CPeriodic对象、定义回调函数及计时器的启动与停止流程。通过具体代码示例展示了如何在类中实现周期性的定时任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 你需要完成最少三个步骤:
一、创建CPeriodic对象,如
Code:
void CPeriodicRunner::StartTimer()
    {
    const TInt tickInterval=1000000;
    iPeriodic=CPeriodic::NewL(0); // neutral priority
    iPeriodic->Start(tickInterval,tickInterval,TCallBack(Tick, this));
    }

CPeriodic::Start原型为:void Start(TTimeIntervalMicroSeconds32 aDelay,TTimeIntervalMicroSeconds32 anInterval,TCallBack aCallBack);
因此我们需要有一个callback函数,在每次回调时能做点什么,于是有了第二个步骤

二、Callback函数的完成
TInt CPeriodicRunner::Tick(TAny* aObject)
{
// cast, and call non-static function
((CPeriodicRunner*)aObject)->DoTick();
return 1;
}

如果您不熟悉TCallBack,可以参考SDK,上面这个函数完成了计时的时候相应的循环动作(DoTick)

三、计时器的析构
在计时停止或外界终止后,需要停止该计时器,一句话即可:
CPeriodicRunner->Cancel();

 

请看下面的例子:

In the header file

// 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 PeriodicTimerCallBack(TAny* aAny);
 
        /**
         * Notice that this is a sample fuction.
         *
         */

        void SomeFunction();
 
    private:    // Member data
 
        /**
         * The periodic timer.
         * Owned by CMyClass
         */

        CPeriodic* iPeriodicTimer;
    };


In the cpp file

// ----------------------------------------------------------------------------
// CMyClass::~CMyClass()
// Destructor.
// ----------------------------------------------------------------------------
//
CMyClass::~CMyClass()
    {
    // ...
 
    if(iPeriodicTimer)
        {
        // Calling Cancel without checking if the timer is active is safe
        iPeriodicTimer->Cancel();
        }
    delete iPeriodicTimer;
 
    // ...
    }
 
// ----------------------------------------------------------------------------
// CMyClass::ConstructL()
// Perform second phase construction of this object.
// ----------------------------------------------------------------------------
//
void CMyClass::ConstructL()
    {
    // ...
 
    // Initialize the periodic timer.
    iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityIdle);
    // Start the periodic timer, when ever the time elapsed
    // the PeriodicTimerCallBack() will get called.
    // Notice: The timer will periodically hit the PeriodicTimerCallBack
    // until you cancel the timer by calling iPeriodicTimer->Cancel().
    iPeriodicTimer->Start(
        KPeriodicTimerInterval5Sec, KPeriodicTimerInterval5Sec, 
        TCallBack(PeriodicTimerCallBack, this));
 
    // ...
    }
 
// ----------------------------------------------------------------------------
// CMyClass::PeriodicTimerCallBack(TAny* aAny)
// The call back function.
// ----------------------------------------------------------------------------
//
TInt CMyClass::PeriodicTimerCallBack(TAny* aAny)
	{
	CMyClass* self = static_cast<CMyClass*>( aAny );
 
        // TODO: The below call is a sample function,
        // you may change it to your requirement.
	self->SomeFunction();
 
        // Cancel the timer when the callback should not be called again.
        // Call: self->iTimerPeriodic->Cancel();
 
	return KErrNone; // Return value ignored by CPeriodic
	}
 
// ----------------------------------------------------------------------------
// CMyClass::SomeFunction(TAny* aAny)
// Notice that this is a sample fuction.
// ----------------------------------------------------------------------------
//
void CMyClass::SomeFunction()
    {
    // TODO: Your code!
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值