Problem with using PostMessage in an multithread application.
Posted on 1998-09-24
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
LRESULT CUIThreadWnd::OnButtonPressed(WPARAM wParam, LPARAM lParam)
sprintf(temp, "Received nID of %d", wParam);
// time-consuming codes
/***** 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.