Overview
Features
Download
Documentation
Community
Add-Ons & Services

Util/Timer cancel call

A general discussion forum.

Util/Timer cancel call

Postby vitaliy » 29 Jul 2010, 15:32

Hi All,

I am looking for some clarification into Poco::Util::Timer cancel call. I have a timer object in my program. I am scheduling certain task to repeat on a set interval. I need to change the interval based on the input. My original approach was to call timer.cancel() and timer.scheduleAtFixedRate right after with the new interval.

What I am observing is if I call to cancel nothing get's scheduled after the call. Timer doesn't execute the task that was scheduled right after the cancel call.

Looking through the sources I see that cancel just schedules a task that clears the queue. I wonder if I have some sort timing issue or cancel is not supposed to be used in this context.

I would appreciate any advice.

Thanks,
Vitaliy
vitaliy
 
Posts: 5
Joined: 29 Jul 2010, 15:24

Re: Util/Timer cancel call

Postby aderouineau » 30 Jul 2010, 00:51

Perhaps try tun use .run() after scheduling it...
aderouineau
 
Posts: 163
Joined: 18 May 2009, 17:38

Re: Util/Timer cancel call

Postby vitaliy » 30 Jul 2010, 01:49

Run is a protected method. And it is an entry point for the thread. If I don't call cancel() scheduled tasks are being executed. The trick is to change the schedule. I may have to recreate the timer but I was curious if I can avoid that.

Thanks for the suggestion.
Vitaliy
vitaliy
 
Posts: 5
Joined: 29 Jul 2010, 15:24

Re: Util/Timer cancel call

Postby aderouineau » 30 Jul 2010, 05:03

Sorry, I hadn't checked the doc when replying.

Perhaps you could create a child class with Timer as parent, and add a member function to reschedule the task (in which case you'd be able to call run).
aderouineau
 
Posts: 163
Joined: 18 May 2009, 17:38

Re: Util/Timer cancel call

Postby vitaliy » 30 Jul 2010, 19:12

Calling run will just block main thread in this case.

Thanks for the suggestion.
Vitaliy
vitaliy
 
Posts: 5
Joined: 29 Jul 2010, 15:24

Re: Util/Timer cancel call

Postby guenter » 30 Jul 2010, 19:39

cancel() works asynchronously, which is a problem if you schedule a new task immediately afterwards.
A better way would be to keep a pointer to the TimerTask, then call cancel() on the TimerTask object and schedule a new TimerTask.
guenter
 
Posts: 1119
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Util/Timer cancel call

Postby vitaliy » 30 Jul 2010, 20:33

Right, I've added a sleep right after cancel as a workaround. But more deterministic approach like canceling the task directly is better.

Thanks,
Vitaliy
vitaliy
 
Posts: 5
Joined: 29 Jul 2010, 15:24

Re: Util/Timer cancel call

Postby aderouineau » 03 Aug 2010, 00:03

Why not use a mutex rather than sleep ?
aderouineau
 
Posts: 163
Joined: 18 May 2009, 17:38


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest