Solved

Problem with using PostMessage in an multithread application.

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 76
Tab names are off by one 5 54
Complete beginner needs help making a cron job 9 118
Smart Camera scanning and reading information 3 107
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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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