现在的位置: 首页 > 综合 > 正文

定时器的实现

2013年12月13日 ⁄ 综合 ⁄ 共 636字 ⁄ 字号 评论关闭

1.在类中定义一个CPeriodic的指针
    CPeriodic* iTimer;

2.定义一个回调函数,回调函数必须是static类型
    函数原型:
    static TInt Loop(TAny* aPtr);
   
    例如实现:
    TInt CFirst::Loop(TAny* aPtr)
    {
    //转换指针
    CFirst* pThis = (CFirst*) aPtr;

    pThis->iBmpShowTime++;

    //大于等于3的时候,就释放定时器
    if (pThis->iBmpShowTime >= 3)
        {
        pThis->iBmpShowTime = 1;
        MEM_FREE(pThis->iTimer);
        pThis->iTimer = NULL;
        }   
    pThis->DrawDeferred();
    }
3.调用的时候,
    //判断定时器是否为空,为空的时候执行
    if (NULL == iTimer)
    {
        iBmpShowTime = 1; //箭头滚动效果次数清零
        //创建定时器
        iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
        iTimer->Start(30000, 30000, TCallBack(Loop, this));
    }

4.析构函数中释放指针
    MEM_FREE(iTimer);

抱歉!评论已关闭.