[Qt-interest] Controlling when timers run

Sean Harmer sean.harmer at maps-technology.com
Thu Jan 14 09:54:26 CET 2010


Hi,

On Thursday 14 January 2010 02:17:27 Glenn Hughes wrote:
> Hello,
> 
> I was wondering if anyone could give me some advice about the following:
> 
> I often want to run various tasks periodically, but only when the
> stack is "fully unwound" i.e. from the first call to exec, not when
> any subsequent exec loops (or other loops) are running, and not when
> other calls have been made into the OS that allow timers to run (for
> whatever reason).
> 
> The motivation is that often timer chores want the program data
> structures to be in a known consistent state, but if the timer gets
> called in the middle of an operation, things may not be as the chore
> code expects, which can easily lead to crashes.
> 
> And other tasks I want to run as much as possible (ignoring all the above).
> 
> Is there any sort of provision for conditionally running timer chores
> like this in Qt?

You could just set a flag that describes whether you are able to safely process 
your data or not. Then check this flag at the start of the slot called by the 
timer.

Alternatively, when you start to update your data, disable the timer (or block 
it's signals), then when your data is in a consistent state restart the timer 
(or unblock its signals).

If your timer-triggered processing takes a long time to complete you can put 
it into a separate thread but that is another can of worms waiting to be 
opened...

HTH,

Sean



More information about the Qt-interest-old mailing list