[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Why doesn't PostThreadMessage work consistently?

Posted on 1998-12-31
12
Medium Priority
?
397 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Accepted Solution

by:
mikeblas earned 300 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

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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 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