Wednesday, April 04, 2007

Handling multiple events using WaitForMultipleObjects() fn

m_hMsgEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hThreadStopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
m_hThread = CreateThread(NULL, 2048, (LPTHREAD_START_ROUTINE)pr_threadStat, (VOID *)this, NULL, &dwID);


DWORD ThreadProc(LPVOID lpParam)
{
CMwsServlet* p = (CMwsServlet*)lpParam;

p->thread();

}

void classname ::thread()
{

HANDLE hEvents[] = { m_hThreadStopEvent, m_hMsgEvent};
DWORD dwObject;
bool bNeedMoreData = false;
// int i, nCondition;

// Main thread loop
while(true)
{

dwObject = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
if (dwObject == 0)
{
return; // Stop event: leave process
}

// If servlet event
if (dwObject == 1)
{
IMWSServletMsgPtr p = m_pServlet->GetNextMessage();

OutputDebugString("Message from Servlet: '");
OutputDebugString(m_pServlet->FormatMessage(p));
OutputDebugString("'\n");
}
}
}

No comments: