//I am writing a program to auto login in a web site. Before making next attempt I have to wait for some time like some
//10 seconds(it is configurable). But during wating UI should not freeze. I wrote code something like this. Here
//event m_hEvent[1]) will be set by another thread after 10 seconds.The problem is UI still freezes some times!
//Any suggestion?
while(1)
{
//m_hEvent[1] will be set by another thread after 10 seconds
dwRet = MsgWaitForMultipleObjects(1, &m_hEvent[1], FALSE, dwMilliseconds, QS_ALLINPUT);
ResetEvent(m_hEvent[1]);

if (dwRet == WAIT_OBJECT_0)
{
break; // The event was signaled
}

//if (dwRet != WAIT_OBJECT_0 + 1)
//break; // Something else happened
if (dwRet == WAIT_TIMEOUT)
{

break; // It is over
}

// There is one or more window message available. Dispatch them
while(PeekMessage(&msg,0,0,0,PM_NOREMOVE))
{
// check for unicode window so we call the appropriate functions
BOOL bUnicode = ::IsWindowUnicode(msg.hwnd);
BOOL bRet;

if (bUnicode)
bRet = ::GetMessageW(&msg, NULL, 0, 0);
else
bRet = ::GetMessageA(&msg, NULL, 0, 0);

if (bRet > 0)
{
::TranslateMessage(&msg);

if (bUnicode)
:ispatchMessageW(&msg);
else
:ispatchMessageA(&msg);
}

if (WaitForSingleObject(m_hEvent[1], 0) == WAIT_OBJECT_0)
{
ResetEvent(m_hEvent[1]);
m_bInMessageLoop = FALSE;
return; // Event is now signaled.
}
}
m_bInMessageLoop = FALSE;