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

来源:互联网 发布:弱视训练软件app 编辑:程序博客网 时间:2024/04/30 04:30
 你需要完成最少三个步骤:
一、创建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!    }
原创粉丝点击