asio::steady_timer无法与“use_awaitable”完成处理程序一起使用。
创始人
2024-11-11 09:30:25
0

使用底层的操作系统定时器(例如Windows API的SetTimer函数)代替asio::steady_timer,然后使用co_await在协程中等待操作完成。以下是示例代码:

#include #include #include #include

using namespace std; using namespace std::experimental;

class TimerAwaitable { public: TimerAwaitable() { timer_id_ = SetTimer(NULL, 0, 1000, &TimerAwaitable::timer_callback_); // 如果使用boost.asio,可以将SetTimer换成asio::steady_timer // timer_.expires_after(chrono::seconds(1)); // timer_.async_wait(TimerAwaitable::timer_callback_); } ~TimerAwaitable() { KillTimer(NULL, timer_id_); // 如果使用boost.asio,可以使用timer_.cancel() } bool await_ready() const { return false; } void await_suspend(coroutine_handle<> hdl) const { coro_handle_ = hdl; } void await_resume() const {

}
static VOID CALLBACK timer_callback_(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
    coro_handle_.resume();
}

private: UINT_PTR timer_id_; static coroutine_handle<> coro_handle_; };

coroutine_handle<> TimerAwaitable::coro_handle_ = nullptr;

int main() { auto timer_awaitable = TimerAwaitable(); for (int i = 0; i < 5; ++i) { co_await timer_awaitable; cout << "tick" << endl; } return 0; }

相关内容

热门资讯

专业透视!hardrock作必... 专业透视!hardrock作必弊!总是真的有辅助神器(有挂方法)-哔哩哔哩1、玩家可以在hardro...
教你透视!哈糖大菠萝挂,wep... 教你透视!哈糖大菠萝挂,wepoker钻石怎么看底牌,方针教程(有挂透视)-哔哩哔哩1、每一步都需要...
解密透视!竞技联盟透视插件!总... 解密透视!竞技联盟透视插件!总是是真的有辅助神器(有挂工具)-哔哩哔哩1、完成竞技联盟透视插件有辅助...
必备透视!wepoker游戏安... 必备透视!wepoker游戏安装教程,wepoker透视脚本免费app,操作教程(真实有挂)-哔哩哔...
关于透视!拱趴大菠萝十三水作必... 关于透视!拱趴大菠萝十三水作必弊!果然是真的有辅助软件(有挂分析)-哔哩哔哩拱趴大菠萝十三水作必弊能...
有挂透视!sohoo开挂辅助,... 有挂透视!sohoo开挂辅助,购买的wpk辅助在哪里下载,指南教程(有挂教程)-哔哩哔哩购买的wpk...
解迷透视!佛手在线大菠萝辅助!... 解迷透视!佛手在线大菠萝辅助!果然一直总是有辅助app(今日头条)-哔哩哔哩1、佛手在线大菠萝辅助免...
曝光透视!pokemmo辅助脚... 曝光透视!pokemmo辅助脚本,pokemmo辅助器手机版下载,资料教程(有挂分享)-哔哩哔哩1、...
总结透视!菠萝德普辅助器免费版... 总结透视!菠萝德普辅助器免费版在哪里!原来真的是有辅助app(有挂猫腻)-哔哩哔哩1、上手简单,内置...
详情透视!wejoker辅助器... 详情透视!wejoker辅助器要钱玩吗,pokerworld修改器,步骤教程(有挂教程)-哔哩哔哩1...