Re: The basics of Windows' messages

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



"Ney André de Mello Zunino" <zunino@xxxxxxxxxxx> wrote in message
news:erlwzAviGHA.456@xxxxxxxxxxxxxxxxxxxxxxx
void processMessages()
{
while (PeekMessage(...))
{
TranslateMessage(...);
DispatchMessage(...);
}
}

Does the pseudo-code above more or less match your explanation?

You are not special-casing WM_QUIT. You should.

If so, then can I assume that /processMessages/ is just a regular function
which will run in the context of the calling thread?

Yes.

In other words, there is actually no magic nor thread switching as in the
way I had initially supposed.

Certainly no magic. :-) Win32 has a pre-emptive scheduler and it will switch
away from your thread when it must. There is not a lot you can (or should)
do about that.

But deeep inside the call to GetMessage(), if there is no message to be
retrieved, then the calling thread will wait. Just as happens if a thread
blocks waiting on an I/O operation, Win32 will move on to schedule something
else.

I did so and learned that WM_QUIT messages are always processed,
regardless of the message filtering used.

Right. So if PeekMessage() removes one you will want to quit. :-) Often, the
simplest strategy is just to repost the message.

I see. I guess I should be more careful then, aware that the word
"yielding" might be used without implying thread switching in a
multiple-thread environment.

I'm not sure that I understand. Lots of things can happen as a result of
calling GetMessage(). GetMessage() was necessary to "yield" in the old days.
The KB article you quite talks about VB's DoEvents() which is a whole other
(smelly <g>) kettle of fish.

I am glad it works that way. I would not be happy to see a supposedly idle
application taking up all of the CPU.

Right. Realize though that the PeekMessage() loop can do just that. Of
course, it will saturate the CPU onlty until the thread's quantum is up. But
if there is no other more pressing thing for Windows to deal with, then the
thread will go back to hogging the machine. That's why you have to be
careful when you use it.

Great. So whenever the WndProc of a given window is called, it can be
assumed that Windows has arranged for the call to take place in the
context of that window's thread.

Yes. Windows and the threads that own them have an "affinity".

Thank you once again, William.

You are welcome.

Regards,
Will


.



Relevant Pages

  • Re: Why RosAsm Breaks on a large number of symbols
    ... >> but the GetMessage might never return. ... A very classic example of polling. ... propogated upwards inside Windows until it determines that the ... to specify GUI or console;) then you could compliment your ...
    (alt.lang.asm)
  • Re: The basics of Windows messages
    ... When the receiver next calls GetMessageor PeekMessage, Windows will note that there is a sent message pending, and it will route it to the traget window before fetching the next message from the queue. ... My confusion comes mainly from the fact that the documentation for those facilities usually state that they make the calling thread yield "execution so the operating system can process other events." ... does the code in /ProcessMessages/ actually cause a switching of the execution context? ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Privilege-escalation attacks on NT-based Windows are unfixable
    ... >>>incoming windows messages. ... >>juju that calls GetMessage until such time as your application does. ... user's desktop without either being the user, or having the privilege to ... privilege escalation attacks are so devastating. ...
    (comp.security.misc)
  • Re: Privilege-escalation attacks on NT-based Windows are unfixable
    ... >>>incoming windows messages. ... >>juju that calls GetMessage until such time as your application does. ... user's desktop without either being the user, or having the privilege to ... privilege escalation attacks are so devastating. ...
    (comp.os.ms-windows.nt.admin.security)
  • Re: f0dders Fabulous Wait States.
    ... > said something like the GetMessage() function remains "suspended" ... "blocking" will make sure your thread takes 0% CPU ... and "block waiting" (in windows terms) means removing your thread ... > it is a data retrieval function, not a polling loop and it is ...
    (alt.lang.asm)