Search code examples
delphiwinapipeekmessagegetmessageidle-processing

Why peekmessage before getmessage?


Why the peekMessage statement is required before Getmessage() for creating message queue?


Solution

  • It's not required.

    What you'll sometimes see, though, is a thread that isn't ready to process messages yet, but it wants to be able to receive them in its message queue. New threads do not have message queues right away, but calling PeekMessage is sufficient to create the message queue. It returns immediately since there is no message, and that allows the thread to continue getting itself ready. In the meantime, other threads can start queuing messages for the new thread. Once the new thread is ready, it calls GetMessage to either retrieve the first message off the queue, or to wait for a message to be put onto the queue.