Re: a *working* PostThreadMessage() implementation...?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Thu, 2 Oct 2008 09:40:01 -0700 (PDT), ".rhavin grobert" <clqrq@xxxxxxxx>
wrote:

On 2 Okt., 18:18, "Doug Harrison [MVP]" <d...@xxxxxxxx> wrote:
On Thu, 2 Oct 2008 05:26:38 -0700 (PDT), ".rhavin grobert" <cl...@xxxxxxxx>
wrote:

On 2 Okt., 00:02, "Doug Harrison [MVP]" <d...@xxxxxxxx> wrote:
I meant it would be better than trying to roll one's own message queue to
work around the PostThreadMessage issues. In order for PTM to work right,
all message loops have to account for it, which could be a problem down the
road if you add a secondary message loop for some reason.

?
there are only two acceptable scenarios:

1: Message IS delivered to the treads message queue and WILL appear in
the threads own message loop

2: Message cant be delivered and the posting-Fn return an error.

Then you should be happy with PostThreadMessage, because it satisfies those
two criteria.

in my experience it doesnt ;-)

That's because you're not interpreting your criteria literally. The
function does what you described, but not what you want. There's a
difference.

Seriously. The problem occurs when one or more message loops
the thread runs fail to handle messages posted by PostThreadMessage.

i've experienced enough cases where the message doesnt even appear.

Appear where? From (1), in the "message queue", or the "threads own message
loop"? I'm rather certain that the message does appear in the "message
queue", and I believe you're not understanding that there really is no such
thing as the "threads own message loop". Any thread can have any number of
message loops, and Windows runs some of its own during certain UI
operations. Thus, the message loop that is currently running IS the
"threads own message loop". The problem with PostThreadMessage is that a
message loop has to be specially constructed to handle "thread messages",
and those secondary message loops inside the OS don't (and can't) handle
"thread messages" and will thus discard them. That's the reason for KB
articles like this one:

PRB: PostThreadMessage Messages Lost When Posted to UI Thread
http://support.microsoft.com/kb/183116

if you use PostMessage to a (hidden) window, any message loop that uses
DispatchMessage will handle things fine. In order to run into a problem
with PostMessage, the message loop would have to eat a message it knows
nothing about, which would be a rare bug.

my current implementation looks like this and im currently testing it
(seems to run):

<snip>

I didn't vet that code beyond noting that you're making things pretty
difficult for yourself. :)

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: a *working* PostThreadMessage() implementation...?
    ... road if you add a secondary message loop for some reason. ... Then you should be happy with PostThreadMessage, ... If the thread doesnt process it right now because it displayes a ... is currently doing $whatever i expect the message queue of my thread ...
    (microsoft.public.vc.mfc)
  • Re: a *working* PostThreadMessage() implementation...?
    ... Then you can't use PostThreadMessage, because it is DEFINED to not work the way you want. ... secondary message loop is now running), then the secondary message loop will simply ... is currently doing $whatever i expect the message queue of my thread ...
    (microsoft.public.vc.mfc)
  • Re: Threads message queue ?
    ... to write your message loop as normal and it will work. ... call to GetMessagefunction creates threads own message queue? ... Otherwise, PostThreadMessage would be meaningless. ... David ...
    (microsoft.public.win32.programmer.ui)
  • Re: a *working* PostThreadMessage() implementation...?
    ... road if you add a secondary message loop for some reason. ... If the thread doesnt process it right now because it displayes a ... is currently doing $whatever i expect the message queue of my thread ... As PostThreadMessage() doesnt fulfill those simple ...
    (microsoft.public.vc.mfc)
  • PostThreadMessage in place of Terminate
    ... use a message loop in your thread to be it more responsive. ... PostThreadMessage may terminate thread immediately. ... Threre is ... yun need to ensure thread has ...
    (borland.public.delphi.language.objectpascal)