为什么要在 getmessage 之前先 peekmessage?
为什么在Getmessage()之前需要使用peekMessage语句来创建消息队列?
Why the peekMessage statement is required before Getmessage() for creating message queue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是必需的。
不过,有时您会看到一个线程尚未准备好处理消息,但它希望能够在其消息队列中接收消息。新线程不会立即拥有消息队列,但调用
PeekMessage
足以创建消息队列。由于没有消息,它会立即返回,这允许线程继续做好准备。与此同时,其他线程可以开始为新线程排队消息。一旦新线程准备就绪,它就会调用GetMessage
来检索队列中的第一条消息,或者等待将消息放入队列中。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 callsGetMessage
to either retrieve the first message off the queue, or to wait for a message to be put onto the queue.它不是。这两个函数做不同的事情。
PeekMessage(...) 不会等待消息出现,它会获取第一条消息(如果存在),也可以选择将其从队列中删除,但如果没有消息,则立即返回 false。这种情况在应用程序中更为常见,您在等待消息时进行一些处理,而不能只是坐在那里永远等待下一条消息。实时游戏等很容易属于这一类。
GetMessage(...) 等待直到有消息,然后获取它。它的 CPU 效率更高,因为它不会不断轮询,但如果没有任何消息,它会暂停。它在我的应用程序和其他不需要持续实时处理的程序中更为常见。
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.
在使用
PeekMessage
之前/而不是GetMessage
有多种原因:PeekMessage
带有PM_REMOVE
标志来轮询消息队列并完全忽略GetMessage
。PM_NOREMOVE
结合使用,并决定是否要处理消息和/或从队列中删除消息。IsWindowUnicode
并选择PeekMessageA
或PeekMessageW
。There are multiple reasons for using
PeekMessage
before/instead ofGetMessage
:PeekMessage
with thePM_REMOVE
flag to poll the message queue and leave outGetMessage
altogether.PM_NOREMOVE
and deciding if you want to process and/or remove the message from the queue, or not.IsWindowUnicode
on the returned messages' window handle and selecting eitherPeekMessageA
orPeekMessageW
.