Solved

Problem with using PostMessage in an multithread application.

Posted on 1998-09-24
3
243 Views
Last Modified: 2013-11-20
I have one main thread (which handles all the GUI functions) and another user-interface thread (this acts as a 'teaching' tool). I want them to communicate with each other.
However, the problem occurs when I post a message from the main thread to the user-interface thread. When I am posting several messages in short interval: it seems the messges still go into the messge queue in chronological order. However, they do not leave the message handlers chronologically (even when they are all posted to the same message handler).

Can anyone please tell me what is going on? And why does this happen?
Do I need to use PostThreadMessage instead of PostMessage??

What I desire is that all the posted messages will be handled one-by-one in a chronological order such that if posted messgaeA, messageB, messageC. Then the program will handle messageA first. After messageA is finished, then handle messageB; and so for.
How can I do this?

I have the following codes:

// Inside the main thread's Cmainview.cpp
void CMainView::OnKeysClicked(UINT nID)
{
      CDigAttApp* pApp = (CDigAttApp*) ::AfxGetApp();
      ::PostMessage(pApp->m_hWnd_otherthread, WM_USER_PRESSEDBUT, nID, 0);
}

// inside the user-interface thread's window
ON_MESSAGE(WM_USER_PRESSEDBUT, OnButtonPressed)

LRESULT CUIThreadWnd::OnButtonPressed(WPARAM wParam, LPARAM lParam)
{
        char temp[300];
      sprintf(temp, "Received nID of %d", wParam);
        // time-consuming codes
      MessageBox(temp);
}

/***** Example of error *****/
When I pressed the button with nID = 1 first, then I pressed the button with nID = 2, then nID=3.
But the message box come out in this order:
Received nID of 3.
Received nID of 2.
Received nID of 1.

Thanks in advance for your generous help.
0
Comment
Question by:e1
  • 2
3 Comments
 

Author Comment

by:e1
Comment Utility
Actually, when I add the following to the code,
LRESULT CUIThreadWnd::OnButtonPressed(WPARAM wParam, LPARAM lParam)
 {
       EnterCounter++;          // a static variable , initialized to zero
       ASSERT(EnterCounter == 1);
            char temp[300];
             sprintf(temp, "Received nID of %d", wParam);
            // time-consuming codes
            MessageBox(temp);
        EnterCounter--;
  }

In my program, an assertion fault occured at that point, which means the message handler is entered twice. But I thought it can only be entered once.
Is there anything wrong with my assumption?
And how do I fix the code so that the message handler can be entered only once?
Thank you very much.

0
 
LVL 3

Accepted Solution

by:
plaroche earned 100 total points
Comment Utility
If you want the message handler to be entered only once you will need to put a critical section around it.

Declare a member variable:

CRITICAL_SECTION   m_cs;

Initialize it once (constuctor is fine):

::InitializeCriticalSection(&m_cs);

Delete it in the destructor:

::DeleteCriticalSection(&m_cs);

In your call add a lock and unlock feature.  Your function body will be protected and call only be entered one at a time:

LRESULT CUIThreadWnd::OnButtonPressed(WPARAM wParam, LPARAM lParam)
 {
  ::EnterCriticalSection(&m_cs);

       EnterCounter++;          // a static variable , initialized to zero
       ASSERT(EnterCounter == 1);
            char temp[300];
             sprintf(temp, "Received nID of %d", wParam);
            // time-consuming codes
            MessageBox(temp);
        EnterCounter--;

  ::LeaveCriticalSection(&m_cs);
  }


If your handler was entered more than once that would explain the message boxes appearing in the wrong order. What you saw was the real order with the last message box on top, appearing first.
Those critical sections will solve the problem.

Not to repeat your error you might try to use the TRACE macro, it would not have shown the same problems than a message box.


0
 

Author Comment

by:e1
Comment Utility
Plaroche, thanks for your answer.
But, can I ask you how to use TRACE. I read the on-line maunal, but it doesn't say how to print it out so that the 'traced' strings can be observed visually.
Thanks.

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now