Solved

Why doesn't PostThreadMessage work consistently?

Posted on 1998-12-31
12
379 Views
Last Modified: 2013-11-20
Help!

I need to send notifications from a worker thread to the main thread of my MFC application.  Using PostThreadMessage usually works, but sometimes the messages never get received.  In particular, if the main thread is displaying a dialog box and the user is moving the dialog box around the screen continuously, any and all messages posted from the worker thread to the main thread using PostThreadMessage never get received by the main thread.

Am I doing something incorrectly or is this just the way PostThreadMessage works?  If the latter, what alternatives do I have?  I do not want to lock up the UI, that is the reason for the worker thread.

Thanks in advance...
0
Comment
Question by:bheld
[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
  • 7
  • 5
12 Comments
 
LVL 11

Expert Comment

by:mikeblas
ID: 1327087
PostThreadMessage() posts a thread message. Special code in the message pump must be executing in order to recognize and appropriately dispatch the thread message.  Since it has no window, it won't work with the regular DispatchMessage() call.

MFC's message pump does it right. But the message pumps built into windows and run during a dialog box, message box, or previewed menu (for example) won't handle the message correctly and can't dispatch it.

B ekiM

0
 

Author Comment

by:bheld
ID: 1327088
Mikeblas,

Thanks for the suggestion...does this hold true for modeless and modal dialog boxes?  I am displaying a modeless dialog box in the main (UI) thread.  Any alternatives?  Perhaps I need to create the dialog box in another thread (not the main thread)?


0
 

Author Comment

by:bheld
ID: 1327089
Mikeblas,

Thanks for the info, but I need to know alternatives in order to solve my problem?
0
Independent Software Vendors: 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!

 
LVL 11

Accepted Solution

by:
mikeblas earned 100 total points
ID: 1327090
You can:

1) Find a way to hook the thread messages.  It's invasive and brutish, so I would only do this as a last resort.

2) You can find a different way to communicate from one thread to another besides using thread messages.  WM_USER+messages are fine, if you know that the threads are in the same process.

3) You can architect your app so that it odesn't send thread messages to threads which are known to have UI. Some people believe that mixing the two (eg, UI which might eat thread messgaes and the requirement of sending thread messages) is to be avoided.  It can work, but you have to be plenty careful.

B ekiM


0
 

Author Comment

by:bheld
ID: 1327091
Mikeblas,

I like your #2 suggestion the best.  Does that suggest I always post messages to windows, or how do I send WM_USER+messages correctly?
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1327092
You'll always post messages to windows, yes.  Since a given thread always owns a given window, you know for sure which thread will handle it and the net effect (including blocking) is the same.

B ekiM
0
 

Author Comment

by:bheld
ID: 1327093
Mikeblas,

I tried you suggestion,

BOOL b = pDoc->MainThread->GetMainWnd()->PostMessage(WM_START_SERVER,(WPARAM)1, (LPARAM)pIteration);

where WM_START_SERVER is defined as "WM_USER + 10"

and the message is still not received.  Any ideas?


0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1327094
What's happening in the thread that's to be receiving the message?  If a Windows message pump is active, you've got no hope of receiving any message.  If it's an MFC message pump, then there's some other problem.

B ekiM
0
 

Author Comment

by:bheld
ID: 1327095
The main thread is displaying a modeless dialog box.  This should be OK, correct?
0
 

Author Comment

by:bheld
ID: 1327096
I forgot to mention that the message does usually get received.  However, it does not get received if the user is moving the modeless dialog box around the screen (the dialog box created by the main thread).


0
 

Author Comment

by:bheld
ID: 1327097
Mike,

Sorry to keep bothering you, but I really need an answer to my problem.  Check my previous comments for more information.

Thanks
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1327098
If the user is moving the box around, Windows is running it's own message pump as it's tracking the mouse and redrawing the window.  You'll notice similar behaviour if the user is tracking a popup menu.

Since Windows, at that point, is running it's own message pump, it will discard non-mouse messages.

B ekiM

0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 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