更好地提高asio deadline_timer示例 [英] Better boost asio deadline_timer example
问题描述
我是一个更好的例子: boost :: asio :: deadline_timer
I'm after a better example of the boost::asio::deadline_timer
将总是超时并调用 close
方法。我尝试在定时器上调用 cancel()
,但会导致传递到 async_wait
的函数被立即调用。
The examples given will always time out and call the close
method. I tried calling cancel()
on a timer but that causes the function passed into async_wait
to be called immediately.
在异步tcp客户端中使用计时器的正确方法是什么?
Whats the correct way working with timers in a async tcp client?
推荐答案
您提到在定时器上调用cancel()会导致传递给async_wait的函数被立即调用。这是预期的行为,但请记住,您可以检查传递给定时器处理程序的错误,以确定定时器是否已取消。如果定时器被取消,则操作_aborted被传递。例如:
You mention that calling cancel() on a timer causes the function passed to async_wait to be called immediately. This is the expected behavior but remember that you can check the error passed to the timer handler to determine if the timer was cancelled. If the timer was cancelled, operation_aborted is passed. For example:
void handleTimer(const boost::system::error_code& error) {
if (error == boost::asio::error::operation_aborted) {
std::cout << "Timer was canceled" << std::endl;
}
else if (error) {
std::cout << "Timer error: " << error.message() << std::endl;
}
}
希望这有助于。如果没有,你正在寻找的具体例子是什么?
Hopefully this helps. If not, what is the specific example that are you looking for?
这篇关于更好地提高asio deadline_timer示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!