-
Re: Maintain process / timer priority when display sleeps
lmcorp Nov 3, 2019 10:36 AM (in response to lmcorp)UPDATE: I found an error, which fixed part of the problem: My timers were in NSRunLoopDefaultMode. Putting them in NSRunLoopCommonModes seems to have fixed the latency problem with the screensaver active. However with display sleep active the latency still occurs (Added some diagnostic messages and am seeing time intervals from about 4 to about 20 seconds for a timer set to repeat every 0.5 seconds.
-
Re: Maintain process / timer priority when display sleeps
Ken Thomases Nov 3, 2019 12:21 PM (in response to lmcorp)First, you're keeping a strong reference to myActivityRef for the duration, right?
Next, I'd try using NSActivityBackground instead of, or perhaps in addition to, NSActivityLatencyCritical. (The latter is documented to say very few apps really need it.) The bit mask values for those two options do not overlap. It's conceivable that NSActivityLatencyCritical without NSActivityBackground is interpreted as only needing lowest latency when in the foreground.
If that doesn't do it, try NSActivityUserInitiated.
-
Re: Maintain process / timer priority when display sleeps
lmcorp Nov 3, 2019 7:18 PM (in response to Ken Thomases)Yes, I do keep the reference to the returned activity object. Indeed, your suggestion of using NSActivityBackground option solved the problem. using NSActivityUserInitiated also works and (as confirmed in ActivityMonitor) also makes the app prevent sleeping, so I don't have to disable sleep system wide.
I found this document, which I think explains what was happening (the system was putting it in App Nap which, as stated, reduces timer firing frequency) https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/AppNap.html#//apple_ref/doc/uid/TP40013929-CH2-SW1
I am still seeing some odd extra latency, for example, when choosing a menu item that opens a window I get a callback times in the range of ~0.8 - 1.1 seconds (the timer interval is 0.5 seconds). I can probably live with this, but it seems like it should do better. Weirdly, when the app is in the background it works BETTER- almost never throws a time over 0.6 seconds (I have it set to log a message if it is over this time). even when choosing menus in the other app, moving windows around, etc.
-
Re: Maintain process / timer priority when display sleeps
Ken Thomases Nov 3, 2019 9:17 PM (in response to lmcorp)Is your timer firing on the main thread? If on a background thread, does the timer callback interact with the main thread? Either of those might explain why GUI operations might interfere with the timer.
-
Re: Maintain process / timer priority when display sleeps
lmcorp Nov 4, 2019 7:24 PM (in response to Ken Thomases)Right Again! detached a new thread to run the timer and now not seeing any latency from user actions. Much better stability overall on the timing accuracy.
-
-
-