CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2013
    Posts
    52

    Unhappy 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;

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Waiting with message loop

    Your code is too hard to read without tags, but I'd probably just use a timer.

  3. #3
    Join Date
    Aug 2013
    Posts
    52

    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

  4. #4
    Join Date
    Aug 2013
    Posts
    52

    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?).

  5. #5
    Join Date
    Aug 2013
    Posts
    52

    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
  •  





Click Here to Expand Forum to Full Width

Featured