I create a ThreadPoolTimer to refresh my token(which will expire in 12 hours), and the timer is expected to be ticked in 12 hours. However, I put my UWP app in background over 12 hours, and then resume the app, the timer is not ticked even though the token is expired. Seems that when the app is background, the ThreadPoolTimer is also suspended. I tried DispatcherTimer, which is not suspended when the app is in background, however it's only available in UI thread. Is there any replacement Timer in UWP can meet my requirement?
This is normal behavior - when the app is suspended, all it's processes are being stopped - take a look at App's Lifecycle.
Your scenario - to run code in 12h interval, fits BackgroundTask with TimeTrigger. Take a look at MSDN and there is also a sample.
Once I've also written a blog post about running such task - maybe will help. Also take a look at this answer at SO.
Related
I am creating an app which should have an internal clock of about 12 hours. Once the clock reaches 0 a button will become enabled in the bottom menu bar. I would also like to display this clock as it is counting down.
I am not sure how I can implement this. I would have to run some kind of background task that keeps checking to see if 12 hours is over, probably using the Date object's getTime() function. What I'm stuck on is I don't know how to run this task in the background. How do I run an asynchronous task throughout the whole app?
Consider a web service, for instance, where user can make an API request to start a task at certain scheduled time. Task definition and scheduled time are persisted in a database.
First approach I came up with is to start a Go timer and wait for the timer to expire in a Goroutine (not blocking the request). This goroutine, after time expiration, will also fire another API request to start executing the task.
Now the problem arises when this service is redeployed. For zero downtime deployment I am using Einhorn with goji. After code reload, obviously both timer goroutine and timer-expiration-handler goroutine dies. Is there any way to recover Go timer after code reload?
Another problem I am struggling with is to allow the user to interrupt the timer (once its started). Go timer has Stop to facilitate this. But since this is a stateless API, when the \interrupt request comes in service doesn't have context of timer channel. And it seems its not possible to marshal the channel (returned from NewTimer) to disk/db.
Its also very well possible that I am not looking at the problem from correct perspective. Any suggestions would be highly appreciated.
One approach that's commonly used is to schedule the task outside your app, for example using crontab or systemd timers.
For example using crontab:
# run every 30 minutes
*/30 * * * * /usr/bin/curl --head http://localhost/cron?key=something-to-verify-local-job >/dev/null 2>&1
Using an external task queue is also a valid option like #Not_a_Golfer mentioned but more complicated.
My goal is to receive updates for some service (using http request-response) all the time, and when I get a specific information, I want to push it to the users. This code must run all the time (let's say every 5 seconds).
How can I run some code doing this when the server is up (means, not by an http request that initiates the execution of this code) ?
I'm using Java.
Thanks
You need to use
Scheduled Tasks With Cron for Java
You can set your own schedule (e.g. every minute), and it will call a specified handler for you.
You may also want to look at
App Engine Modules in Java
before you implement your code. You may separate your user-facing and backend code into different modules with different scaling options.
UPDATE:
A great comment from #tx802:
I think 1 minute is the highest frequency you can achieve on App Engine, but you can use cron to put 12 tasks on a Push Queue, either with delays of 5s, 10s, ... 55s using TaskOptions.countdownMillis() or with a processing rate of 1/5 sec.
I have a requirement where I need to show a Lock screen , when the app remains idle for certain period of time. In WP7 is there any way to get the Idle time(Any OS APIs). I have gone through MSDN documentation of PhoneApplicationService.UserIdleDetectionMode and PhoneApplicationService.ApplicationIdleDetectionMode but it is just for enabling and disabling.I need some method by which I can get the Idle time. Thanks in Advance
To my knowledge, there isn't a way of doing that in the current API version.
You could implement a timer in your application and reset it every time there is an user interaction (with your controls) within your application.
The bigger question is how you should handle the OS lock screen? I mean, if your application has an in-app custom lock screen that has kicked in, and then the OS lock screen kicks in, then the user has to unlock 2 screens. Not especially user friendly in that case.
I have a task queue with several tasks. If I delete a particular task from Admin Console, it disappears from the task queue but GAE doesnt terminate it. The task is still being executed in the background.
Is this a common behavior ?
Yeah, I see the same behavior. Seems you can only delete pending tasks from the admin console. Once they've started they continue to run until they finish or hit an exception (could be as long as 10 minutes with the new update).
I've noticed they don't stop on version upgrades either, which is a little weird if you aren't expecting it... if the task takes a long time you end up with handlers running in two versions of the app simultaneously. It makes sense though.