📄 timers.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "Timers.hpp"
#include "Common.hpp"
/*------------------------------------------------------------------------------
Timers_t::Timers_t
constructor
------------------------------------------------------------------------------*/
Timers_t::Timers_t() :
m_ListenerWindow(NULL)
{
;
}
/*------------------------------------------------------------------------------
Timers_t::Timers_t
desstructor
------------------------------------------------------------------------------*/
Timers_t::~Timers_t()
{
if (m_TimerList.size() != 0)
{
m_TimerList.clear();
}
}
/*------------------------------------------------------------------------------
Timers_t::Initialize
Initialize the Timers_t object
Parameters:
ListenerWindow: [in] the handle of a listener window
Returns: S_OK when succeeds, otherwise failed
------------------------------------------------------------------------------*/
HRESULT
Timers_t::Initialize(
HWND ListenerWindow
)
{
m_ListenerWindow = ListenerWindow;
return S_OK;
}
/*------------------------------------------------------------------------------
Timers_t::AddTimer
Add a new timer and timer handler to the list
Parameters:
TimeOutValue: [in] the time out value in seconds
pTimerHandler: [in] the pointer to an ITimerHandler_t object
pTimerIdentifier: [out] the pointer to a UINT indicates the unique timer identifier
Returns: S_OK when succeeds, otherwise failed
------------------------------------------------------------------------------*/
HRESULT
Timers_t::AddTimer(
UINT TimeOutValue,
ITimerHandler_t* pTimerHandler,
UINT* pTimerIdentifier
)
{
ASSERT(pTimerHandler != NULL);
ASSERT(pTimerIdentifier != NULL);
//initialize the out parameter
*pTimerIdentifier = 0;
//allocate a new timer from heap
TimerHandlerMapping_t NewTimer = {0, pTimerHandler};
//always push the new timer at the end of the timer list
if (! m_TimerList.push_back(NewTimer))
{
return E_OUTOFMEMORY;
}
//get the address of the new timer as the timer ID so that it won't collide with any existing one
m_TimerList.back().TimerIdentifier = reinterpret_cast<UINT>(&(m_TimerList.back()));
UINT Result = SetTimer(
m_ListenerWindow,
m_TimerList.back().TimerIdentifier,
TimeOutValue,
NULL
);
if (Result == 0)
{
RemoveTimer(m_TimerList.back().TimerIdentifier);
return CommonUtilities_t::GetErrorFromWin32();
}
//pass out the timer identifier
*pTimerIdentifier = m_TimerList.back().TimerIdentifier;
return S_OK;
}
/*------------------------------------------------------------------------------
Timers_t::RemoveTimer
Remove an existing timer and timer handler from the list
Parameters:
TimerIdentifier: [in] the id of the timer
Returns: S_OK when succeeds, otherwise failed
------------------------------------------------------------------------------*/
HRESULT
Timers_t::RemoveTimer(
UINT TimerIdentifier
)
{
for (TimerList_t::iterator Timer = m_TimerList.begin();
Timer != m_TimerList.end(); Timer++)
{
if ((*Timer).TimerIdentifier == TimerIdentifier)
{
m_TimerList.erase(Timer);
KillTimer(m_ListenerWindow, TimerIdentifier);
return S_OK;
}
}
return E_INVALIDARG;
}
/*------------------------------------------------------------------------------
Timers_t::OnTimerEvent
This gets called when the listener window gets the WM_TIMER mesg. We dispatch the
timer event to corresponding timer handler in our list
Parameters:
TimerID: [in] the id of the timer
------------------------------------------------------------------------------*/
void
Timers_t::OnTimerEvent(
UINT TimerIdentifier
)
{
ITimerHandler_t* pHandler = NULL;
for (TimerList_t::iterator Timer = m_TimerList.begin();
Timer != m_TimerList.end(); Timer++)
{
if ((*Timer).TimerIdentifier == TimerIdentifier)
{
pHandler = (*Timer).pTimerHandler;
break;
}
}
if (pHandler != NULL)
{
pHandler->OnTimerExpires(TimerIdentifier);
//pHandler might be removed from the list when the OnTimerExpires() is called
pHandler = NULL;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -