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

Why doesn't PostThreadMessage work consistently?

Posted on 1998-12-31
12
371 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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: 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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

792 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