为什么要在 getmessage 之前先 peekmessage?

发布于 2024-09-01 11:53:20 字数 50 浏览 4 评论 0原文

为什么在Getmessage()之前需要使用peekMessage语句来创建消息队列?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青衫负雪 2024-09-08 11:53:20

这不是必需的。

不过,有时您会看到一个线程尚未准备好处理消息,但它希望能够在其消息队列中接收消息。新线程不会立即拥有消息队列,但调用 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 calls GetMessage to either retrieve the first message off the queue, or to wait for a message to be put onto the queue.

一指流沙 2024-09-08 11:53:20

它不是。这两个函数做不同的事情。

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.

云裳 2024-09-08 11:53:20

在使用 PeekMessage 之前/而不是 GetMessage 有多种原因:

  1. 确保程序在消息到达之前不会挂起 - 这有点多余,因为您可以直接使用 PeekMessage 带有 PM_REMOVE 标志来轮询消息队列并完全忽略 GetMessage
  2. 将该函数与 PM_NOREMOVE 结合使用,并决定是否要处理消息和/或从队列中删除消息。
  3. 在返回消息的窗口句柄上调用 IsWindowUnicode 并选择 PeekMessageAPeekMessageW
  4. 以上的倍数。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文