A third-party library provides a function I need to call every 100ms. Setting up a timer to do that works very well as long as my app is on foreground. When my app is on background timer works for a while but after a about a minute timer is called only after 10 second delay. The same happened when I created a separate thread with usleep-function. Is there any way I can keep timer running while my app is on background?
Use beginActivityWithOptions:reason: to disable app nap for your application.
You should try and re-architect to avoid these sorts of frequent timers, especially when your application is in the background. Making this sort of change will improve battery life for your users.
Related
Actually, I'm new to 'xamarin.mac' applications and I'm trying an application that takes screenshot frequently also captures system idle time on login.So far I've managed to capture screenshot but How to take system idle time?
I've gone through 'NSEvents'.But I cannot figure it out how to implement those
event against button click.
I just want the system idle time count that starts on login.
I'm making a Clock app in swift for OS X. I have a Timer in my Clock app that works kinda like the one in iOS. My problem is whenever I put my app into the background or minimize it the application kinda freezes or stops counting after being in the background for a minute or so. It starts working right where it left off when I bring it into the foreground. Is there anyway I can make my application run in the background? I've looked and haven't found anything that works.
Thanks to anyone who can help.
Are you relying on the timer to "count" the seconds? You should only use the timer to provoke an update of the display. When the timer fires, you should check the actual time and calculate what should be displayed based on that time, not how many times your timer has fired. It should never be the case that your app "starts working right where it left off" (if, by that, you mean that it shows the same time as when it was backgrounded and starts counting up from there again).
What's probably happening is that your app is being put into App Nap. Also see the Energy Efficiency Guide for Mac Apps.
How can I manage to keep my activity in the foreground when I come back from sleeping mode?
I tried this (but it looks like it's only used to prevent the device to sleep)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
What do you mean by "sleep" mode? In a normal situation, your activity goes away and your watch-face takes over. If you don't want the watch-face to take over but want the activity to be in low-powered mode, you can have your activity go into ambient mode by extending WearableActivity and calling setAmbientEnabled() and use the relevant callbacks to stay informed about when your app enters the ambient mode to avoid using excessive CPU, and to resume normal operation when you exit the ambient mode.
I have a windows 8 store app and a Background task is also associated with it. Everything working fine. And my question is, is there any possibility to close my app from BackgroundTask Run method ?
thanks for suggestions.
No, You can't do that instantly!
But you could save a file in your app's local storage -or settings- and let the foreground app check for this file every 10 minutes -whatever- and close the app in a specific condition ..
Your background task can be hosted in its own dedicated process or it can be hosted in the same process as your UI. In a separate process your foreground app can open and close without impacting your background process. In the shared process when your foreground app closes, so does the background task. This is not true in reverse, the shared process model does not allow a closing background task to close the foreground app. Too bad, huh?
Technically, it is not recommend that a foreground application close itself. But, hey, that API is there for something, right? When the background task shares the process with the foreground app then it can communicate directly between them with shared memory. This would introduce method 1 for communicating from your background task to your foreground app - probably by using a static event.
If that's not what you want, and you need separate processes, then your options are a bit more tricky.
Here's the best option:
You set a special setting (let's say it's ApplicationData.LocalSettings.Values["DataFromBackground"] = "PleaseExit") and then call the ApplicationData.SignalDataChanged method from the background task which will raise the ApplicationData.DataChanged event handled by the foreground app. How much lag will there be? I am not sure, but there will be some, so be ready for that.
Be sure an remember to set DataFromBackground back to some empty value, including calling Value.Remove() so you don't mistakenly process it again. That being said, you should also poll for that value when your application launches (or resumes) in case your background task wrote it while the event could not be heard.
This is probably the easiest way to implement communication.
Make sense? I speak more on this in my Ignite session on the topic.
Best of luck!
I would like to secure an app of mine with a lockscreen. Currently, it's shown in didBecomeActive, but it seems as if that's a little bit too late as the content is visible for a short time sometimes before the lockscreen is shown.
As the lockscreen is not always shown (e.g. only if the app was suspended for more that 30 seconds), I don't want to add it in applicationWillResignActive.
One method would be to start a timer then and add the lockscreen after 30 seconds in the background, but I don't like that solution, I would prefer a method like WILLbecomeActive- is there a way to achieve that?