一个简单的Symbian OS异步计时器

本文介绍了一个简单的Symbian异步计时器实现方法,通过继承CTimer并重写RunL()和DoCancel()方法来完成计时任务。文章提供了完整的代码示例,并介绍了如何使用两阶段构造器进行初始化。

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

先看一些资料吧,尤其是SDK里面自带的例子,很有用的,其实异步计时器并不向一开始想象的那么复杂。基本思想:写一个类继承CTimer,然后重写它的RunL()和DoCancel()方法;在需要的地方启动计时器,然后在Runl()方法里处理计时结束的任务,或在DoCancel()里面取消计时。注意由于CTimer从CActive继承而来,所以在实现计时器的构造函数里面要给它指定优先级。当然,为了便于管理各个类,最好再实现一个检测计时器的M类。

闲话就不说了,下面是我的计时器的代码。

//一个简单的异步计时器。

//-----.h
#ifndef __TIMEOUTTIMER_H__
#define __TIMEOUTTIMER_H__

// INCLUDES
#include <e32base.h>
class MTimeOutNotifier;

// CLASS DECLARATION

class CTimeOutTimer : public CTimer
    {
    public: // Constructors and destructors

        static CTimeOutTimer* NewL( const TInt aPriority,MTimeOutNotifier& aTimeOutNotify);

        static CTimeOutTimer* NewLC( const TInt aPriority,MTimeOutNotifier& aTimeOutNotify);

        virtual ~CTimeOutTimer();

    protected: // Functions from base classes

        void RunL();

    private: // Constructors and destructors

        CTimeOutTimer( const TInt aPriority,MTimeOutNotifier& aTimeOutNotify);

        void ConstructL();

 private://data
  MTimeOutNotifier& iNotify;
    };

#endif // __TIMEOUTTIMER_H__


//----------.cpp
// INCLUDE FILES
#include <aknnotewrappers.h>

#include "TimeOutTimer.h"
#include "TimeOutNotifier.h"

// ========================= MEMBER FUNCTIONS ==================================

// -----------------------------------------------------------------------------
// CTimeOutTimer::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTimeOutTimer* CTimeOutTimer::NewL( const TInt aPriority ,MTimeOutNotifier& aTimeOutNotify)
    {
    CTimeOutTimer* self = CTimeOutTimer::NewLC( aPriority, aTimeOutNotify);
    CleanupStack::Pop( self );
    return self;
    }

// -----------------------------------------------------------------------------
// CTimeOutTimer::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTimeOutTimer* CTimeOutTimer::NewLC( const TInt aPriority ,MTimeOutNotifier& aTimeOutNotify)
    {
    CTimeOutTimer* self = new ( ELeave ) CTimeOutTimer( aPriority, aTimeOutNotify);
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

// -----------------------------------------------------------------------------
// CTimeOutTimer::CTimeOutTimer()
// C++ default constructor can NOT contain any code, that might leave.
//learn about member initialization list first!
// -----------------------------------------------------------------------------
//
CTimeOutTimer::CTimeOutTimer( const TInt aPriority,
                              MTimeOutNotifier& aTimeOutNotify )
: CTimer( aPriority ), iNotify( aTimeOutNotify )
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CTimeOutTimer::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CTimeOutTimer::ConstructL()
{
    CTimer::ConstructL();
    CActiveScheduler::Add( this );
}
// -----------------------------------------------------------------------------
// CTimeOutTimer::~CTimeOutTimer()
// Destructor.
// -----------------------------------------------------------------------------
//
CTimeOutTimer::~CTimeOutTimer()
    {
    // No implementation required
    }

// -----------------------------------------------------------------------------
// CTimeOutTimer::RunL()
// Called when operation completes.
// -----------------------------------------------------------------------------
//
void CTimeOutTimer::RunL()
    {
    // Timer request has completed, so notify the timer's owner
  CAknConfirmationNote* note=new(ELeave)CAknConfirmationNote();
  note->ExecuteLD(_L("TimeOut!"));
  iNotify.TimerExpired();
    }

 

//关于类MTimeOutNotifier的定义
//------.h
#ifndef __TIMEOUTNOTIFIER_H__
#define __TIMEOUTNOTIFIER_H__

// CLASS DECLARATION
/**
* MTimeOutNotifier
*  This class specifies the function to be called when a timeout occurs.
*  Used in conjunction with CTimeOutTimer class.
*/
class MTimeOutNotifier
    {
    public: // New functions

        /**
        * TimerExpired.
        * The function to be called when a timeout occurs.
        */
        virtual void TimerExpired() = 0;
    };

#endif // __TIMEOUTNOTIFIER_H__


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值