Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why peekmessage before getmessage?

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

like image 488
Josh Avatar asked May 17 '10 15:05

Josh


People also ask

What is the difference between PeekMessage and GetMessage?

The main difference between the two functions is that GetMessage does not return until a message matching the filter criteria is placed in the queue, whereas PeekMessage returns immediately regardless of whether a message is in the queue.

What does PeekMessage do?

PeekMessage retrieves messages associated with the window identified by the hWnd parameter or any of its children as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters.

What is the difference between GetMessage and DispatchMessage function?

GetMessage pulls the WM_LBUTTONDOWN message from the queue and fills in the MSG structure. Your program calls the TranslateMessage and DispatchMessage functions. Inside DispatchMessage, the operating system calls your window procedure. Your window procedure can either respond to the message or ignore it.

Is GetMessage blocking?

According to here, GetMessage() is a blocking call which won't return until there's a message can be retrieved from the message queue.


3 Answers

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.

like image 155
Rob Kennedy Avatar answered Sep 28 '22 07:09

Rob Kennedy


It's not. The two functions do different things.

PeekMessage(...) doesn't wait for a message to appear -- it gets the first one if it's there, optionally removing it from the queue as well, but returns false immediately there isn't one. It's more common in apps where you're doing some processing while waiting for messages, and can't just sit there and wait forever for the next message. Real-time games and such easily fall into this category.

GetMessage(...) waits til there's a message, and gets it. It's more efficient CPUwise, cause it's not constantly polling, but it will pause if there aren't any messages. It's more common in formy apps and other programs that don't require constant real-time processing to be going on.

like image 36
cHao Avatar answered Sep 28 '22 06:09

cHao


There are multiple reasons for using PeekMessage before/instead of GetMessage:

  1. Ensuring the program will not hang until a message arrives - that's a bit redundant, cause you can directly use PeekMessage with the PM_REMOVE flag to poll the message queue and leave out GetMessage altogether.
  2. Using the function with PM_NOREMOVE and deciding if you want to process and/or remove the message from the queue, or not.
  3. Calling IsWindowUnicode on the returned messages' window handle and selecting either PeekMessageA or PeekMessageW.
  4. Multiple of the above.
like image 45
Viktor Svub Avatar answered Sep 28 '22 07:09

Viktor Svub