timer.cpp

来自「实现了无线传感器网络中的一个路由协议算法。」· C++ 代码 · 共 76 行

CPP
76
字号
// Timer.cpp: implementation of the CTimer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SimSensor.h"
#include "Timer.h"
#include "Msg.h"
#include "Node.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTimer::CTimer()
{
   m_uID = NULL;
   m_Type = ONCE;
}

CTimer::~CTimer()
{

}

void CTimer::SetMyTimer(UINT uElapse,bool Type)
{   
	m_Type = Type;
	m_uID = (UINT)timeSetEvent(uElapse,0,(LPTIMECALLBACK)TimeProc,(DWORD)this,(Type?TIME_ONESHOT:TIME_PERIODIC)); 
	if (m_uID==NULL) TRACE("timeSetEvent fail!\n");
}


void CTimer::SendTimerMsg(int MsgType)
{
   	CMsg *pMsg ;
	pMsg = new CMsg;
	pMsg->Type = MsgType;               //collectdata Msg
	((CNode *)m_pNode)->MsgBuff.InsertMsg(pMsg);
}

void CTimer::Band(LPVOID pNode)
{
    m_pNode = pNode;
}

void CTimer::EndTimer()
{
    if (m_uID) {
	    timeKillEvent(m_uID);
	    m_uID = NULL;
	}
}

VOID CALLBACK CTimer::TimeProc(
  UINT uID,      
  UINT uMsg,     
  DWORD dwUser,  
  DWORD dw1,     
  DWORD dw2      
)
{
     CTimer *pTimer = (CTimer *)dwUser;
     if ( ONCE == pTimer->m_Type ) {
         //pTimer->SendTimerMsg (100);
         ::MessageBox(NULL,"Timer ONCE arrived!","hehe",MB_OK);
	 }else
 		 //pTimer->SendTimerMsg (101);
 		 ::MessageBox(NULL,"Timer CONTINUE arrived!","hehe",MB_OK);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?