windows 非窗口定时器

#ifndef _MYTIMER_H_
#define _MYTIMER_H_
#include <map>
#include <set>
#include <windows.h>
#include <Mmsystem.h>
#include <string>

#include "ttype.h"
#include "lock.h"

#pragma comment(lib, "Winmm.lib")

#define  TIMER_PERIOD 500

class CMyTimer;
enum emTimeType
{
    E_TIME_ONESHOT = 0,
    E_TIME_PERIOD  = 1
};

struct TIMERVAL
{
    UINT uType;
    UINT uTimeVal;
    UINT uTimeFinal;
    CMyTimer *pCTimer;
};


typedef   std::map<UINT,TIMERVAL>   TimerMap;   

class CMyTimer
{
public:
    CMyTimer(void);
    ~CMyTimer(void);
public:  
    void   KillMyTimer();  
    void   SetOneTimer(UINT nTimeID,UINT nElapse);
    void   SetTimer(UINT nTimeID,UINT nElapse);

    static void   SetMyTimerOnce(UINT   nElapse,DWORD dwUser = NULL);
    static void   SetMyTimerPeriod(UINT nElapse,DWORD dwUser = NULL);  
    virtual bool  ontime(UINT nTimeID) = 0;

    static CLock    m_lock;
    static       bool       m_bInitTime;
private:
    MMRESULT   m_nTimerID;
    
    static void    CALLBACK OnTime(UINT uTimerID, UINT uMsg, 
                            DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
    static TimerMap TimerMap;

};

#endif 
#include "MyTimer.h"
#include <iostream>
#include <time.h>

using namespace std;
CMyTimer::CMyTimer(void)
{
    m_nTimerID = 0;
}


CMyTimer::~CMyTimer(void)
{

}

TimerMap CMyTimer::TimerMap;
bool CMyTimer::m_bInitTime = false;
CLock    CMyTimer::m_lock;

void  CMyTimer::SetMyTimerOnce(UINT nElapse,DWORD dwUser)
{
    if (m_bInitTime)
    {
        return;
    }
    if (NULL !=  ::timeSetEvent(nElapse,1,OnTime,dwUser,TIME_ONESHOT))
    {
        m_bInitTime = true;
    }
}

void  CMyTimer::SetMyTimerPeriod(UINT nElapse,DWORD dwUser)
{
    if (NULL != ::timeSetEvent(nElapse,1,OnTime,dwUser,TIME_PERIODIC))
    {
        m_bInitTime = true;
    }
}

void    CALLBACK CMyTimer::OnTime(UINT uTimerID, UINT uMsg, 
                                     DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
    CSignedLock lock(&m_lock);
    for (auto iter = TimerMap.begin();
            iter != TimerMap.end();)
    {
        iter->second.uTimeVal += TIMER_PERIOD;
        if (iter->second.uTimeVal >= iter->second.uTimeFinal)
        {
            if (E_TIME_ONESHOT == iter->second.uType) 
            {
                iter->second.pCTimer->ontime(iter->first);
                TimerMap.erase(iter++);
                continue;
            }
            if (E_TIME_PERIOD == iter->second.uType)
            {
                iter->second.uTimeVal = 0;
                if (false == iter->second.pCTimer->ontime(iter->first))
                {
                    TimerMap.erase(iter++);
                    continue;
                }
            }
        }
        iter++;
    }
    return;
}

void  CMyTimer:: KillMyTimer()
{
    ::timeKillEvent(m_nTimerID);
    m_bInitTime = false;
}

void  CMyTimer::SetOneTimer(UINT nTimeID,UINT nElapse)
{
    CSignedLock lock(&m_lock);
    auto iter = TimerMap.find(nTimeID);
    if (iter == TimerMap.end())
    {
        TimerMap[nTimeID].pCTimer = this;
        TimerMap[nTimeID].uTimeFinal = nElapse;
        TimerMap[nTimeID].uTimeVal = 0;
        TimerMap[nTimeID].uType = E_TIME_ONESHOT;        
    }
}

void   CMyTimer::SetTimer(UINT nTimeID,UINT nElapse)
{
    CSignedLock lock(&m_lock);
    auto iter = TimerMap.find(nTimeID);
    if (iter == TimerMap.end())
    {
        TimerMap[nTimeID].pCTimer = this;
        TimerMap[nTimeID].uTimeFinal = nElapse;
        TimerMap[nTimeID].uTimeVal = 0;
        TimerMap[nTimeID].uType = E_TIME_PERIOD;
    }
}
#ifndef _Work_H_
#define _Work_H_
#include "MyTimer.h"

enum MyEnum
{
    E_TIME_ID_1 = 1,
    E_TIME_ID_2 = 2,
    E_TIME_ID_3 = 3,

};


class Work :public CMyTimer
{
public:
    Work(void);
    ~Work(void);
    bool Init();
    void Run();
    bool unInit();
    void onTime();
    bool time_fun_1();
    bool time_fun_2();
    bool time_fun_3();

    bool ontime(UINT nTimeID);
};

#endif//_Work_H_
#include "Work.h"
#include <iostream>
using namespace std;

Work::Work(void)
{
}


Work::~Work(void)
{

}

bool Work::Init()
{
    if (!CMyTimer::m_bInitTime)
    {
        CMyTimer::SetMyTimerPeriod(TIMER_PERIOD);
    }    
    return true;
}

void Work::Run()
{
    SetOneTimer(E_TIME_ID_1,1000);
    SetTimer(E_TIME_ID_2,1000);
}

bool Work::unInit()
{
    return true;
}

bool Work::time_fun_1()
{
    std::cout<<"只执行一次的定时器"<<__LINE__<<std::endl;
    return true;
}

bool Work::time_fun_2()
{
    std::cout<<"每周期执行一次的定时器"<<__LINE__<<std::endl;
    static int i = 0;
    if (++i == 3)
    {
        return false;
    }
    return true;
}

bool Work::time_fun_3()
{
    std::cout<<__LINE__<<std::endl;
    return true;
}

//
bool Work::ontime(UINT nTimeID)
{
    switch (nTimeID)
    {
    case E_TIME_ID_1:
        return time_fun_1();
    case E_TIME_ID_2:
        return time_fun_2();
    case E_TIME_ID_3:
        return time_fun_3();
    default:
        break;
    }
    return true;
}
///普通锁定类
#ifndef _CLOCK_H_
#define _CLOCK_H_

#include <windows.h>
class  CLock
{
    friend class CSignedLock;

private:
    CRITICAL_SECTION                m_csLock;                    ///关键段

    ///函数定义
public:
    ///构造函数
    inline CLock() { ::InitializeCriticalSection(&m_csLock); }
    ///析构函数
    inline ~CLock() { ::DeleteCriticalSection(&m_csLock); }

    ///功能函数
private:
    ///锁定函数
    inline void Lock() { ::EnterCriticalSection(&m_csLock); }
    ///解锁函数 
    inline void UnLock() { ::LeaveCriticalSection(&m_csLock); }
};

class  CSignedLock
{
    ///变量定义
private:
    CLock        * m_pLock;                    ///锁定对象
    int            m_iLockCount;                ///锁定计数
    ///函数定义
public:
    ///构造函数
    CSignedLock(CLock * pLock)
    {
        m_iLockCount = 0;
        m_pLock = pLock;
        Lock();
    }
    ///析构函数
    ~CSignedLock()
    {
        UnLock();
    }

public:
    ///锁定函数
    inline void Lock()
    {
        if (m_pLock)
        {
            m_iLockCount++;
            m_pLock->Lock();
        }
    }
    ///解锁函数 
    inline void UnLock()
    {
        if (m_pLock)
        {
            m_iLockCount--;
            m_pLock->UnLock();
        }
    }
};

#endif //_CLOCK_H_
#ifndef _TTYPE_H_
#define _TTYPE_H_

typedef  unsigned int UINT;

#define  saferelease(p) {\
    if(nullptr != p) {\
        delete p ; \
        p = nullptr;\
    }\
}

#endif
#include "work.h"
#include <windows.h>
#include <iostream>
using namespace std;

Work work ;

int main()
{
    work.Init();
    work.Run();
    system("pause");
    cout<<endl;
}

 

posted @ 2015-11-05 20:19  湾仔小新  阅读(468)  评论(0编辑  收藏  举报