gpt4 book ai didi

c++ - Boost.Asio Win32 Windows 应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:32 25 4
gpt4 key购买 nike

我使用 Visual Studio 的向导创建了一个新的 Win32 Windows 应用程序。并将 Boost.Asio 添加到项目中。我想用它的 deadline_timer 每秒做一些重复的工作。虽然计时器(及其线程)运行良好,但我鼓励了 UI 未更新的问题,因为 io_service.run() 阻塞了 UI 线程。快速搜索后,我找到了 this thread .其实是一样的问题!因此,我将我的代码移到 GetMessage() 循环中并将 io_service.run() 更改为 io_service.poll_one()。

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
//Windows main loop stuff
if (io.stopped())
io.reset();
io.poll_one();
}

好吧,它可以工作,但只有在有 Windows 消息等待时才能工作,这很明显。还有什么我可以做的吗?或者我应该使用 WinAPI 线程而不是 boost?

最佳答案

它不像 Boost 那样可移植,但 Windows 已经提供了与消息循环集成的不错的事件 API。

您可以使用 CreateWaitableTimer 并将 GetMessage 替换为 MsgWaitForMultipleObjects。然后循环将为传入消息或计时器唤醒。也可以使用其他类型的 handle 。

当然,总有 SetTimer 会导致您收到 WM_TIMER 消息。但我更喜欢可等待的计时器。

关于c++ - Boost.Asio Win32 Windows 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21654834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com