Execute task even in the background [duplicate]

与世无争的帅哥 提交于 2020-01-02 09:11:44

问题


Possible Duplicate:
How to run application in background in Windows Phone?

I am investigating Windows Phone 8 SKD for examples on how to create a task (FTP write to be precise) that would be executed periodically even if the application is in the background.

The documentation points me to Background Agents because the Background Transfer Service "only supports transfers using HTTP and HTTPS. FTP is unsupported". I have been trying and testing the samples, everything is working (more or less) except for the fact that Background Agent event time cannot be setup. According to the documentation "Periodic agents typically run every 30 minutes" and no method/class/constructor has a time/date parameter. You have ScheduledActionService.LaunchForTest(task, Time) but it is to be used only during debug time, not in production, and can make MS reject your app from the market.

Is there any solution or workaround to make my agent fire up every minute?


回答1:


You cannot make your app execude code every 1 minute when it's not runnig in foreground. Usually you would use for background execution so called PeriodicTask or ResourceIntensiveTask. The first one runs every ~28 minute, can use no more than 6MB of memory (11MB on WP8) and must finish the execution within ~20 seconds. The second one can run for longer time and use more memory, but it has some tight requirements like phone on Wi-Fi signal, on charger, etc...

Another thing you can do on WP8 is implementing Location Tracking app that can run in background if it's actively tracking user's location. If your app is location based, you can use this and download whatever you want every 1 minute. But if your app is not location based, you're most likely out of luck.

How to use location tracking and background execution in WP8:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681691(v=vs.105).aspx




回答2:


No, the WP8 platform doesn't really support background tasks in the way you're thinking. This is a deliberate attempt to limit damaging battery-life by having applications running in the background.

Background Agents are the solution provided if you really need to run something when your application isn't active, but they aren't something you can schedule.

The platform expects you to switch to a model where polling operations are replaced by push notifications and as much significant processing as possible is executed off the device.



来源:https://stackoverflow.com/questions/13647584/execute-task-even-in-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!