Timer period incorrect when display sleeps

Hi All,

I have a project which has been working well under MacOS X.5, and I want to bring it into the new world of Xojo, which needs OSX.7 or better.

It uses a Timer with period of 2 seconds to check the clock, and when the clock gets to a certain determined time, to do things. I set the host iMac computer to never go to sleep, but to sleep the display only. Hard drives were allowed to spin down when possible.

Under Leopard (OSX.5) this worked well while the display was dark (i.e. asleep, no screen saver running). The timer would action every 2 seconds and do its thing at the appointed time.

Under OSX.9.5, this behaviour is changed. While the screen saver is running, the timer period is as it should be, as evidenced by writing to a log. When the display goes dark (i.e. display sleep), the timer period appears to change to something much longer - 22 seconds is the longest, but it varies between ten seconds and 22 seconds.

It would seem this is the OS helpfully reducing power to my app, by giving it fewer slices of the processor (I am guessing here). Is there a way to reverse this and let my app continue to work as it used to ?

I have filed a bug report, but this may not be a Xojo problem …

Regards,
Tony Barry

it’s a feature.

You can use NSTimerMBS in our plugin and set tolerance yourself.
Also NSProcessInfoMBS lets you declare activity, so system knows that it should not sleep.

Xojo.Core.Timer also has a tolerance property, so maybe it is worth trying.

Thank you to Christian and Paul for answers.

My tests indicated that during display sleep the timer period became quite erratic. The default condition of period = 2000 msec would expand to between 10 and 22 seconds during display sleep.

Setting xojo.core.tolerance to 200 msec did not improve this - the timer would go to sleep for around 10 seconds, then wake three times with two second periods, then go to sleep for another ten or so seconds.

I did not have access to the SNProcessInfoMBS due to the project requiring an older plugin family. This might have been the way to go if I were able to take advantage of it.

My workaround was to change the code flow in such a way that I could use

xojo.core.timer.callLater(numMSECtoWait, addressOf routineToCall)

This (new) functionality saved my bacon. While its precision is not great during sleep (±5 sec), the callLater is a very nice way to provide a long timing distance wake up call similar to a cron job, but from within Xojo.

Regards,
Tony Barry
Sydney, AUS

I’m not sure what version of OS X you using when you say “under MacOS X.5”
Is that 10.9.5 ? 10.10.5 ?
Or something else
There reason this may be relevant is that in some versions of OS X they do some tricks to use less power when things are sleeping and this could be a side effect
Timers are not precision timers - they are set to raise their events “no sooner than X milliseconds” but there can be reasons that they are delayed

And the tolerance is a “hint” not a “requirement” - the OS can still ignore that

Hi Norman,

MacOS X.5 was called Leopard I believe. Old.

I am now bringing the app into the world of MacOS X.9 or better. I think that is called Mavericks.

Yes this is an OS helpful feature to reduce power consumption. For most purposes the behaviour would be fine.

Yes timers are not precision timers and I can get along without too much precision.

Yes the tolerance made some improvement but not quite what I wanted.

I don’t think this is a Xojo issue. It may not be an issue at all. More like a feature that isn’t wanted in this case :slight_smile:

Regards,
Tony Barry
Sydney, AUS

There’s no version called OS X .5 - there is a 10.5 though

Again the version is 10.9 no “OS X.9”

It just makes understanding which version you’re referring to difficult when you state the version is an unconventional fashion like that
The version from the “About This Mac” box is commonly the best way to refer to it - no misunderstandings that way

[quote=215193:@Tony Barry]
Yes this is an OS helpful feature to reduce power consumption. For most purposes the behaviour would be fine.

Yes timers are not precision timers and I can get along without too much precision.

Yes the tolerance made some improvement but not quite what I wanted.

I don’t think this is a Xojo issue. It may not be an issue at all. More like a feature that isn’t wanted in this case :slight_smile:

Regards,
Tony Barry
Sydney, AUS[/quote]

There are some declares that CAN be used to tell the OS NOT to do this (basically to not throttle your app down) but then in OS X 10.11 your app gets flagged as a CPU hog :slight_smile:

Ah. I see. Well I shall try to be understandable in my descriptions.

The original version ran under 10.5.
The new version will run under 10.9 and higher.
I would prefer not to be labelled as a CPU hog. That’s uncool.

Regards,
Tony Barry
Sydney, AUS

It could be https://en.wikipedia.org/wiki/Timer_coalescing
This is enabled in Mavericks (OS X 10.9)
There are several technologies that could affect this
http://arstechnica.com/apple/2013/06/how-os-x-mavericks-works-its-power-saving-magic/

Any or all of them could be whats going on here