定时器的封装

定时器的封装可以通知窗体,可以不通知窗体。

1 定义

typedef void (CALLBACK *TimerProc)(HWND hwnd,UINT uMsg,    UINT_PTR idEvent,DWORD dwTime);

class CMyTimer
{
public:
    CMyTimer();
    ~CMyTimer();

public:
    bool start(unsigned int idEvent, DWORD dwTime, TimerProc pTimerProc);
    void stop();

private:
    bool m_running;
    unsigned int m_nTimer;
};

 2 实现


CMyTimer::CMyTimer()
{
    m_running = false;
    m_nTimer = 0;
    //::SetTimer(NULL, idEvent, dwTime, pTimerProc);
}

bool CMyTimer::start(unsigned int idEvent, DWORD dwTime, TimerProc pTimerProc)
{
    m_nTimer = ::SetTimer(NULL, idEvent, dwTime, pTimerProc);
    m_running = true;
    if (0 == m_nTimer)
    {
        DWORD drCode = GetLastError();
        char msg[100] = { 0 };
        sprintf_s(msg, 100, "创建定时器出错,出错码为:%d\n", drCode);
        OutputDebugStringA(msg);
        m_running = false;
    }
    if (m_running)
    {
        OutputDebugStringA("定时器启动了");
    }
    return m_nTimer;
}

void CMyTimer::stop()
{
    if (m_nTimer)
    {
        ::KillTimer(NULL, m_nTimer);
        m_running = false;
        OutputDebugStringA("定时器关闭了");
    }
    
}

CMyTimer::~CMyTimer()
{
    if (m_running)
    {
        stop();
    }
}
3 运用

A CMyTimer m_MyTimer;

B 时间通知函数的实现,如下:

定时器函数定义与实现:

static void CALLBACK TimerProc(
        HWND hwnd,
        UINT uMsg,
        UINT_PTR idEvent,
        DWORD dwTime
        );

VOID CALLBACK CCHClientSocket::TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
    OutputDebugStringA("\n TimerProc run...\n");
    CAutoLock AutoLock1(&GetInstance().m_EventLock);
    if (GetInstance().m_Opreate.GetDataListLen() == 0)
    {
        Sleep(100);
        return;
    }
    
    stTimerEvent TimerEvent = GetInstance().m_Opreate.PopData();
    switch (TimerEvent.curEvent)
    {
    case emConnect:
    {
        if (GetInstance().m_ConnectCallBack)
        {
            char msg[100] = { 0 };
            sprintf_s(msg, 100, "\n--TimerProc emConnect errorcode:%d \n", TimerEvent.nError);
            GetInstance().m_ConnectCallBack(TimerEvent.nError);
            OutputDebugStringA(msg);
        }
    }
        break;
    case emClose:
    {
        if (GetInstance().m_CloseCallBack)
        {
            GetInstance().m_CloseCallBack();
        }
    }
        break;
    case emSendData:
    {
        if (GetInstance().m_ReciveDataCallBack)
        {
            string strData = (char*)TimerEvent.MsgData.pData;

            LPSTR pData = (LPSTR)strData.c_str();
            string strmsg = "回调数据为:";
            strmsg += pData;

            wstring wstr;
            CCharactor::StringToWString(strData, wstr);

            
            cdebugstr debugstr((char*)strmsg.c_str(), 1);

            stReveData ReveData;
            ReveData.pReveData = (LPTSTR)wstr.c_str();
            ReveData.nlen = TimerEvent.MsgData.nLen;

            GetInstance().m_ReciveDataCallBack(&ReveData, TimerEvent.nError);

            //GetInstance().m_ReciveDataCallBack((LPTSTR)wstr.c_str(), TimerEvent.MsgData.nLen, TimerEvent.nError);
        }
    }
        break;
    default:
        break;
    }

}

C 开启时钟,将上面定义的函数指针传递进去

#define TimerEventID 100

m_MyTimer.start(TimerEventID, 100, TimerProc);

D 关闭时钟

m_MyTimer.stop();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值