Hello, I want some function / functor to run every n seconds, is there right timer in boost for that? I tried to loop deadline_timer but wasn't able to get it work more than one time. Here is part of code I tryied: //bool _isOnline; // status of internet connection. //boost::asio::io_service _ioService; // need for boost timers. //boost::asio::deadline_timer _internetConnectionTimer; void Logic::InitializeInternetConnectionChecking( long secondsInterval ) { _internetConnectionTimer.expires_from_now( boost::posix_time::seconds( secondsInterval ) ); _internetConnectionTimer.async_wait( boost::bind( &Logic::CheckInternetConnection, this ) ); _ioService.poll(); } void Logic::InitializeInternetConnectionChecking() { InitializeInternetConnectionChecking( 5 ); } void Logic::CheckInternetConnection() { if ( _isOnline = IsLanOrRasInetConnect() ) onOnline(); else onOffline(); _ioService.reset(); InitializeInternetConnectionChecking(); }
Hello, I want some function / functor to run every n seconds, is there right timer in boost for that? I tried to loop deadline_timer but wasn't able to get it work more than one time.
You have to re-start the timer after it expires, i.e. call again expires_from_now(), async_wait() in the handler.
On Thursday 12 February 2009 18:45:50 Igor R wrote:
Hello, I want some function / functor to run every n seconds, is there right timer in boost for that? I tried to loop deadline_timer but wasn't able to get it work more than one time.
You have to re-start the timer after it expires, i.e. call again expires_from_now(), async_wait() in the handler.
Didn't I do this in sample code?
You have to re-start the timer after it expires, i.e. call again expires_from_now(), async_wait() in the handler.
Didn't I do this in sample code?
ah... I didn't pay attention to the sample code :)). Well, the problem is that poll() - according to the asio reference - just processes *ready* handlers, w/o blocking(), so timer isn't waited. So, as for your sample code, it shouldn't work even once, because noone waits for the timer - but problably in your actual code there're some other parts that run io_service and that's why it works sometimes. However, if you call run() instead of poll() then the execution will be blocked until the timer fires, and your sample code will work as you expect.
On Thursday 12 February 2009 20:02:03 Igor R wrote:
You have to re-start the timer after it expires, i.e. call again
expires_from_now(), async_wait() in the handler.
Didn't I do this in sample code?
ah... I didn't pay attention to the sample code :)). Well, the problem is that poll() - according to the asio reference - just processes *ready* handlers, w/o blocking(), so timer isn't waited. So, as for your sample code, it shouldn't work even once, because noone waits for the timer - but problably in your actual code there're some other parts that run io_service and that's why it works sometimes. However, if you call run() instead of poll() then the execution will be blocked until the timer fires, and your sample code will work as you expect.
If I use run there application hang, it doesn't use CPU and doesn't eat memory, just stop responce.
If I use run there application hang, it doesn't use CPU and doesn't eat memory, just stop responce.
io_service::run() blocks until all the work is done. However, you can run io_service in another thread, like this: boost::thread thread(boost::bind(&io_service::run, &io_service_)); //note that io_service_ must outlive the thread then you can use this io_service with any number of timers/sockets - all their handlers will run in that thread.
On Thursday 12 February 2009 23:12:52 Igor R wrote:
If I use run there application hang, it doesn't use CPU and doesn't eat memory, just stop responce.
io_service::run() blocks until all the work is done. However, you can run io_service in another thread, like this: boost::thread thread(boost::bind(&io_service::run, &io_service_)); //note that io_service_ must outlive the thread
then you can use this io_service with any number of timers/sockets - all their handlers will run in that thread.
Thank you, this helps and I think I will use it for sockets as well.
On Thursday 12 February 2009 20:02:03 Igor R wrote:
You have to re-start the timer after it expires, i.e. call again
expires_from_now(), async_wait() in the handler.
Didn't I do this in sample code?
ah... I didn't pay attention to the sample code :)). Well, the problem is that poll() - according to the asio reference - just processes *ready* handlers, w/o blocking(), so timer isn't waited. So, as for your sample code, it shouldn't work even once, because noone waits for the timer - but problably in your actual code there're some other parts that run io_service and that's why it works sometimes. However, if you call run() instead of poll() then the execution will be blocked until the timer fires, and your sample code will work as you expect.
I got it, I wasn't thinking that run blocks application, I just need second thread for that stuff.
participants (2)
-
Alexei Sergeev
-
Igor R