-
August 25th, 2013, 08:30 AM
#1
Waiting with message loop
//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;
-
August 25th, 2013, 08:34 AM
#2
Re: Waiting with message loop
Your code is too hard to read without tags, but I'd probably just use a timer.
-
August 25th, 2013, 09:22 AM
#3
Re: Waiting with message loop
Sorry. I missed some part of the code. Now it is working. But I have doubt of deadlock between threads. If it is there
I will need your help. Time being things are OK. Thank you
-
August 25th, 2013, 10:08 AM
#4
Re: Waiting with message loop
There is some problem with my logic in synchronizing the threads. Before going to that I want to explore the possibility
of using a waitable timer. When I press the "Stop" button I should be able to quit the message loop. In my code
it is a matter of calling SetEvent. But on waitable timer handle I cannot call SetEvent()(or Can I?).
-
August 26th, 2013, 08:26 AM
#5
Re: Waiting with message loop
I used a combination of Event and Waitable timer. Now it is working fine. Thank you for your suggestion for using Timer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|