Solved

Why doesn't PostThreadMessage work consistently?

Posted on 1998-12-31
12
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
Mikeblas,

Thanks for the info, but I need to know alternatives in order to solve my problem?
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bheld
Comment Utility
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
Comment Utility
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
Comment Utility
The main thread is displaying a modeless dialog box.  This should be OK, correct?
0
 

Author Comment

by:bheld
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
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…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now