bheld
asked on
Why doesn't PostThreadMessage work consistently?
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...
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...
ASKER
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)?
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)?
ASKER
Mikeblas,
Thanks for the info, but I need to know alternatives in order to solve my problem?
Thanks for the info, but I need to know alternatives in order to solve my problem?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
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
B ekiM
ASKER
Mikeblas,
I tried you suggestion,
BOOL b = pDoc->MainThread->GetMainW nd()->Post Message(WM _START_SER VER,(WPARA M)1, (LPARAM)pIteration);
where WM_START_SERVER is defined as "WM_USER + 10"
and the message is still not received. Any ideas?
I tried you suggestion,
BOOL b = pDoc->MainThread->GetMainW
where WM_START_SERVER is defined as "WM_USER + 10"
and the message is still not received. Any ideas?
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
B ekiM
ASKER
The main thread is displaying a modeless dialog box. This should be OK, correct?
ASKER
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).
ASKER
Mike,
Sorry to keep bothering you, but I really need an answer to my problem. Check my previous comments for more information.
Thanks
Sorry to keep bothering you, but I really need an answer to my problem. Check my previous comments for more information.
Thanks
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
Since Windows, at that point, is running it's own message pump, it will discard non-mouse messages.
B ekiM
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