Re: The basics of Windows' messages
- From: "William DePalo [MVP VC++]" <willd.no.spam@xxxxxxxx>
- Date: Thu, 8 Jun 2006 12:44:43 -0400
"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
.
- References:
- The basics of Windows' messages
- From: Ney André de Mello Zunino
- Re: The basics of Windows' messages
- From: William DePalo [MVP VC++]
- Re: The basics of Windows' messages
- From: Ney André de Mello Zunino
- Re: The basics of Windows' messages
- From: William DePalo [MVP VC++]
- Re: The basics of Windows' messages
- From: Ney André de Mello Zunino
- The basics of Windows' messages
- Prev by Date: Re: Vectored exceptions
- Next by Date: Re: token manipulation under Vista Beta 2
- Previous by thread: Re: The basics of Windows' messages
- Next by thread: Re: The basics of Windows' messages
- Index(es):
Relevant Pages
|