VC++多线程应用--代码清单六:定时

本文介绍了一个基于Windows API的周期性定时器类CTimerCycle及事件池类CTimeUpEventPool的设计与实现。CTimerCycle负责创建周期性的定时任务,并通过回调函数触发事件;而CTimeUpEventPool则用于管理事件及其触发条件,实现了事件的注册、触发和回收机制。
#ifndef __TIMERCYCLE_H
#define __TIMERCYCLE_H

#pragma once

#include 
".EventDefine.h"

class __declspec(dllexport) CTimerCycle
{
public:
    CTimerCycle(
void);
    
~CTimerCycle(void);

    
void Initial(OBJECT);
    
    
void StartTimerCycle(void);
    
    
void StopTimerCycle(void);

protected:
    
    
static void CALLBACK OnTimeProc(UINT, UINT, DWORD, DWORD, DWORD);
    
    
void SetTimerCyclicHandler(void);

private:
    UINT    m_TimerID;
    UINT    m_TimerRes;
    
static OBJECT  eventpf_p;

}
;

#endif

 

 

#include "StdAfx.h"
#include 
<mmsystem.h>
#include 
".TimerCycle.h"
#include 
".EventPF.h"

OBJECT CTimerCycle::eventpf_p 
= NULL;

CTimerCycle::CTimerCycle(
void)
    : m_TimerID(NULL)
    , m_TimerRes(NULL)
{
}


CTimerCycle::
~CTimerCycle(void)
{
    StopTimerCycle();
    m_TimerID 
= 0;
    m_TimerRes 
= 0;
}


void CTimerCycle::Initial(OBJECT evpf_p)
{
    eventpf_p 
= evpf_p;
    StartTimerCycle();
}


void CTimerCycle::StartTimerCycle(void)
{
    TIMECAPS tc;

    
if(timeGetDevCaps(&tc,sizeof(TIMECAPS))==TIMERR_NOERROR){
        m_TimerID 
= min(max(tc.wPeriodMin,m_TimerID),tc.wPeriodMax); 
        timeBeginPeriod(m_TimerID);
        SetTimerCyclicHandler();
    }

    
}


void CTimerCycle::StopTimerCycle(void)
{
    timeKillEvent(m_TimerRes);
    timeEndPeriod(m_TimerID);
}


void CALLBACK CTimerCycle::OnTimeProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
    
if (NULL != eventpf_p){
        ((CEventPF
*)eventpf_p)->Clock();
    }

}


void CTimerCycle::SetTimerCyclicHandler(void)
{
    m_TimerRes 
= timeSetEvent(TICKBASETIME, m_TimerID, (LPTIMECALLBACK)OnTimeProc, (DWORD)0,TIME_PERIODIC);
}

 

 

 

#ifndef _TIMEUPEVENTPOOL_H
#define _TIMEUPEVENTPOOL_H

#pragma once

#include 
".EventPool.h"

class __declspec(dllexport) CTimeUpEventPool
{
public:
    CTimeUpEventPool(
void);
    
~CTimeUpEventPool(void);

    
    
void Initial(CEventPool*);
    
    US GetUniqueId(US);
    
    
void RaiseEvent(UL, US, US, CEventPara*);
    
    
void ClockTimeUpEventList(void);
    
    
void Free(void);

private:
    
static US            m_UniqueId;
    CEventPool
*            eventpool_p;
    CCircularLinkList    m_TimeUpEventList;
    CCriticalSection    m_CriticalSection;
}
;


#endif/* _TIMEUPEVENTPOOL_H */

 

#include "StdAfx.h"
#include 
".TimeUpEventPool.h"


#define        Msec_to_Ticks(n)    (UL)(n < TICKBASETIME ? 1 : (n / TICKBASETIME + 1))

US CTimeUpEventPool::m_UniqueId 
= 0;

CTimeUpEventPool::CTimeUpEventPool(
void)
    : eventpool_p(NULL)
{
}


CTimeUpEventPool::
~CTimeUpEventPool(void)
{
}


void CTimeUpEventPool::Initial(CEventPool* obj)
{
    m_CriticalSection.Lock();
    eventpool_p 
= obj;
    m_UniqueId 
= 0;
    m_CriticalSection.Unlock();
}


US CTimeUpEventPool::GetUniqueId(US size)
{
    US result;

    m_CriticalSection.Lock();

    result 
= ( ++m_UniqueId % (US_MAX - size) ) + size;

    m_CriticalSection.Unlock();

    
return result;
}


void CTimeUpEventPool::RaiseEvent(UL time, US size, US trig, CEventPara* para_p)
{
    CMyEvent
*            myEvent_p;
    CEventElement
*        element_p;

    m_CriticalSection.Lock();
    myEvent_p 
= new CMyEvent();
    element_p 
= new CEventElement();
    myEvent_p
->m_InitialTick = Msec_to_Ticks(time);
    myEvent_p
->m_NowTick = Msec_to_Ticks(time);
    myEvent_p
->m_TrigMax = size; 
    myEvent_p
->m_Trigger = trig;
    myEvent_p
->parameter_p = para_p;
    element_p
->Set(myEvent_p);
    m_TimeUpEventList.AppendTail(element_p);

    m_CriticalSection.Unlock();
}


void CTimeUpEventPool::ClockTimeUpEventList(void)
{
    CNote
*    rootnode_p;
    CNote
*    node_p;
    CMyEvent
*        myEvent_p;
    CEventPara
*        eventPara_p;
    m_CriticalSection.Lock();

    
if (m_TimeUpEventList.GetHeader(&rootnode_p)){
        
while( NULL != rootnode_p ) {
            myEvent_p 
= (CMyEvent*)rootnode_p->element_p->Get();
            
            myEvent_p
->m_NowTick--;
            
if (0 >= myEvent_p->m_NowTick){
                
/*Copy the Parm*/
                myEvent_p
->parameter_p->Copy(&eventPara_p);
                
/*On The Time, Raise Event*/
                
this->eventpool_p->RaiseEvent(TASK_RANK_HIGH,myEvent_p->m_Trigger,eventPara_p);

                node_p 
= rootnode_p;
                
/*next node*/
                rootnode_p 
= m_TimeUpEventList.GetNext(rootnode_p);
                
/*delete the node*/
                m_TimeUpEventList.Remove(node_p);
            }

            
else{
                rootnode_p 
= m_TimeUpEventList.GetNext(rootnode_p);
            }


        }

    }

    m_CriticalSection.Unlock();
}


void CTimeUpEventPool::Free(void)
{
    m_CriticalSection.Lock();
    m_TimeUpEventList.Free();
    m_UniqueId 
= 0;
    m_CriticalSection.Unlock();
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值