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

呵呵,第一次在Blog上写东西,不说紧张吧,但许多东西却是不知怎么写,慢慢来吧,请同行的各位前辈以后多执教!

前几天写了由于要写一个录音和回放的程序,所以要使用到计时器;但一开始用的是同步计时器,结果发现出现了许多问题,因为同步计时器启动的时候,会屏蔽所有的用户命令;真是很郁闷。以前就有人建议我写一个简单的异步计数器,但当时决觉得太麻烦(其实是对Symbian C++的知识了解不够),一直托了下来。现在看来是麻烦要写,不麻烦也要写了。

先看一些资料吧,尤其是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__

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值