Play-/Source/ThreadMsg.cpp
jpd002 0c5fb983fd Moved remotely
git-svn-id: http://svn.purei.org/purei/trunk@40 b36208d7-6611-0410-8bec-b1987f11c4a2
2006-06-15 04:19:30 +00:00

63 lines
1.2 KiB
C++

#include "ThreadMsg.h"
CThreadMsg::CThreadMsg()
{
m_nMessage = false;
m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
}
CThreadMsg::~CThreadMsg()
{
CloseHandle(m_hEvent);
}
unsigned int CThreadMsg::SendMessage(unsigned int nMsg, void* pParam)
{
unsigned long nStatus;
MSG wmmsg;
m_Msg.nMsg = nMsg;
m_Msg.pParam = pParam;
m_Msg.nRetValue = 0;
m_nMessage = true;
ResetEvent(m_hEvent);
while(1)
{
nStatus = MsgWaitForMultipleObjects(1, &m_hEvent, FALSE, INFINITE, QS_SENDMESSAGE);
if(nStatus == WAIT_OBJECT_0) break;
//Process the message in queue to prevent a possible deadlock
if(PeekMessage(&wmmsg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&wmmsg);
DispatchMessage(&wmmsg);
}
}
return m_Msg.nRetValue;
}
bool CThreadMsg::GetMessage(MESSAGE* pMsg)
{
if(!m_nMessage) return false;
if(pMsg != NULL)
{
pMsg->nMsg = m_Msg.nMsg;
pMsg->pParam = m_Msg.pParam;
}
return true;
}
void CThreadMsg::FlushMessage(unsigned int nRetValue)
{
if(!m_nMessage) return;
m_nMessage = false;
m_Msg.nRetValue = nRetValue;
SetEvent(m_hEvent);
}
bool CThreadMsg::IsMessagePending()
{
return m_nMessage;
}