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
Solved

Problem with using PostMessage in an multithread application.

Posted on 1998-09-24
3
250 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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