Solved

Why doesn't PostThreadMessage work consistently?

Posted on 1998-12-31
12
363 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 120
Replacement selected text 2 61
How to determine file rename from EventLog ? 2 49
matchUp  challenge 9 105
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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