Solved

Problem with using PostMessage in an multithread application.

Posted on 1998-09-24
3
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:e1
ID: 1322553
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
ID: 1322554
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
ID: 1322555
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

718 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