Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with using PostMessage in an multithread application.

Posted on 1998-09-24
3
Medium Priority
?
256 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 300 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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