|
#include "stdafx.h"
#include "Clock.h"
#include "SrvAgent.h"
#include "service.h"
unsigned int Clock::count=0;
CPtrArray Clock::ptrArray;
Clock::Clock(){
this->ptrArray.SetSize(100);
}
unsigned int Clock::GetSeqNum()
{
static unsigned int g_nSeqNum = 1;
return g_nSeqNum++;
}
void Clock::GetTime(int *pHour, int *pMinute, int *pSecond)
{
CTime t = CTime::GetCurrentTime();
*pHour = t.GetHour();
*pMinute = t.GetMinute();
*pSecond = t.GetSecond();
}
void Clock::GetDate(int *pYear, int *pMonth, int *pDay)
{
CTime t = CTime::GetCurrentTime();
*pDay = t.GetDay();
*pMonth = t.GetMonth();
*pYear = t.GetYear();
}
/******************************************************************************************
DESCRIPTION: Time out call back function.
INPUT:
OUTPUT:
RETURN:
REMARK: idEvent is the timer sequence number.
*******************************************************************************************/
void CALLBACK TimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
)
{
MSG_TIMER_T TimerData;
void* pDestPort=Clock::GetDestPort(idEvent);
TimerData.nSeqNum = idEvent;
AgtPostExtEvent(EVENT_ON_TIMER, &TimerData, sizeof(MSG_TIMER_T),pDestPort);
}
/******************************************************************************************
DESCRIPTION: Set a timer.
INPUT: nElapse: time-out value in milli-seconds.
pDestPort: the destination port.
OUTPUT: *pSeqNum: the sequence number of the new timer. Time out event data contains this value.
RETURN: The timer ID. Pass this value to ClkKillTimer to destroy this timer.
*******************************************************************************************/
unsigned int Clock::SetTimer(unsigned int nElapse, void *pDestPort, unsigned int * pSeqNum)
{
unsigned int nTimerID = GetSeqNum();
unsigned int result=::SetTimer(NULL, nTimerID, nElapse, TimerProc);
CLOCK_DATA_T* pData=new CLOCK_DATA_T;
pData->pDestPort=pDestPort;
pData->seqClkNum=nTimerID;
pData->sysClkNum=result;
ptrArray.SetAt(count,static_cast<void*>(pData));
count++;
return result;
}
void* Clock::GetDestPort(unsigned int sysClkNum){
for(unsigned int i=0;i<count;i++){
CLOCK_DATA_T* pData=static_cast<CLOCK_DATA_T*>(ptrArray.GetAt(i));
if(sysClkNum==pData->sysClkNum)
return pData->pDestPort;
}
return NULL;
}
/******************************************************************************************
DESCRIPTION: Kill a timer.
INPUT: The timer ID
OUTPUT:
RETURN: TRUE if succeeds, otherwise FALSE.
*******************************************************************************************/
BOOL Clock::KillTimer()
{
bool flag=true;
for(unsigned int i=0;i<count;i++){
CLOCK_DATA_T* pData=static_cast<CLOCK_DATA_T*>(ptrArray.GetAt(i));
if(!::KillTimer(NULL,pData->sysClkNum))
flag=false;
delete pData;
}
return flag;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jerome. (Free to speak, free to use.)