c++ - 了解 Windows API 中消息处理的不同策略

标签 c++ winapi

我正在开展一个家庭项目,以使用 DirectX 和 WINAPI。我刚发现一个有趣的行为。

在下面的代码中,如果我使用我的 wndd->h_(类型:HWND),CreateWindowEx() 的结果是什么,在 PeekMessage()作为第二个参数,然后主循环开始在负载较重的单核上工作,同时将第二个参数保留为0,然后计算展开得很好。

void WinLoop() {
    while (!wndd->exit_) {
      //if (PeekMessage(&(wndd->msg_), 0, 0, 0, PM_REMOVE)) {        //NOTE : Frequently changing what core is loaded
        if (PeekMessage(&(wndd->msg_), wndd->h_, 0, 0, PM_REMOVE)) { //NOTE : Heavy load on one core
            TranslateMessage(&(wndd->msg_));
            DispatchMessage(&(wndd->msg_));
        }

        //TODO : update
        //TODO : draw
        //TODO : calculate statistic
    }
}

似乎一切正常,但我找不到任何相关信息。引擎盖下是什么?

最佳答案

根据 the documentation对于 PeekMessage 函数,第二个参数是...

hWnd [in, optional]

Type: HWND

A handle to the window whose messages are to be retrieved. The window must belong to the current thread.

If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

If hWnd is -1, PeekMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by PostMessage (when the hWnd parameter is NULL) or PostThreadMessage.

注意PeekMessage 函数的作用 也很重要。简而言之,它检索存在于队列中的第一条消息(匹配指定条件,如果适用)。因为您指定了 PM_REMOVE 标志,所以它也从队列中删除该消息。

更有趣的是如果队列中没有给您的消息会发生什么。在这种情况下,PeekMessage 立即返回 false。

您现在有足够的信息来理解两个函数调用之间的行为差​​异。在第一种情况下,您将 NULL 作为第二个参数传递,因此 PeekMessage 函数正在检索属于 任何 窗口的消息当前线程,以及线程消息。在第二种情况下,您将句柄作为第二个参数传递给特定窗口,因此 PeekMessage 函数检索消息那个窗口。因为与所有窗口和线程相比,该窗口要检索的消息要少得多,所以 PeekMessage 函数大多只是返回 false。几乎没有时间花在处理消息上。一旦它返回 false,您的 while 循环就会开始,循环回到开头并再次调用 PeekMessage 函数。

基本上,您已经创建了一个紧密循环,它只是坐着消耗 CPU 时间,不断轮询 PeekMessage 函数而不任何事情,这会干扰操作系统的执行调度程序。当然,该应用仍然可以被抢占,但您可以保证使用 100% 的分配时间片。

这种行为是有目的的,就像写游戏一样。 PeekMessage 循环也常用于正在进行长时间后台处理但希望继续发送消息以保持 UI 响应的应用程序。但是没有理由在不需要实时处理的普通应用程序的消息循环中使用 PeekMessage。相反,您需要 GetMessage 函数,该函数实际上等待直到它收到一条消息才返回。这在 CPU 时间方面要高效得多,因为它不是不断轮询。你得到了你需要的时间,但仅此而已。 It is also much more battery-friendly .

我假设,如果您正在玩 DirectX,您可能正在尝试编写游戏或屏幕保护程序或其他一些准实时的东西,这就是您使用PeekMessage 函数放在首位。但是您可能希望将 NULL 作为函数的第二个参数传递,因为您希望处理所有窗口和线程消息,而不仅仅是主窗口的消息。

关于c++ - 了解 Windows API 中消息处理的不同策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34962000/

相关文章:

c++ - 在 Windows XP 中以编程方式旋转显示? (C++/Qt 和 WindowsAPI)

c++ - 使用10.4u SDK的`does not name a type`帮助(10.6.8)

c++ - 统一2个空间内存

c++ - 如何在执行回调时隐藏顶级 GTK 窗口

delphi - 寻找进程间通信中使用的 Windows 消息的替代方案

python - 是否可以在 python 中在运行时检查 chromedriver.exe 版本?

c++ - 如何在 winapi 标准对话框中处理键盘事件?

c++ - 在提取 SFX 存档时显示进度条

c++ - win32原始键盘输入删除自动重复

c++ - std::exception_ptr 存储的生命周期要求