2

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?

Community
  • 1
  • 1
MLProgrammer-CiM
  • 17,231
  • 5
  • 42
  • 75
  • 1
    This use case is not supported. The windows phone 8 platform has restrictions on running background tasks to preserve battery life. – kindasimple Nov 30 '12 at 15:07
  • What are you trying to achieve with your regularly scheduled activity? There's likely a way to do it without the need for a background agent. – Paul Turner Nov 30 '12 at 15:08
  • Unfortunately, there is no way to make an application run in the background more frequently than every 30 minutes. – JoshVarty Nov 30 '12 at 15:08
  • @Tragedian My app has real-time tracking with one poll every minute, and I want to send several string messages via TCP with every position recorded once every 5 minutes. – MLProgrammer-CiM Nov 30 '12 at 15:37
  • What precisely are you tracking? If it's Geolocation, there are additional classes to support it. – Paul Turner Nov 30 '12 at 15:52
  • Geolocation, yes. Getting my points is not the problem I'm describing. Sending them using TCP is. It has to be TCP, not HTTP, nor rest, nor file-sync. And as battery-efficient as possible, but in a reasonable schedule, not every 30 minutes. – MLProgrammer-CiM Nov 30 '12 at 15:53
  • See my answer to the same question on WP8 full multitasking @ http://stackoverflow.com/questions/13514064/how-to-run-application-in-background-in-windows-phone/13520869#13520869 – JustinAngel Nov 30 '12 at 20:17
  • That was not the question. I KNOW how to run background task, I want to change the behavior to less than 30 minutes or a workaround. Why do SO keep closing questions without fully reading them? – MLProgrammer-CiM Dec 02 '12 at 02:43

2 Answers2

5

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

Martin Suchan
  • 10,600
  • 3
  • 36
  • 66
  • I understand geolocation stuff, I have been tracking myself in the background for a while. Now, I want to send that data to my web service that has to be FTP. Are you suggesting to use my onLocationChanged method? – MLProgrammer-CiM Dec 02 '12 at 02:41
  • My problem is not how to implement an agent, there are plenty of very specific tutorials, is setting them up to fire every minute. 60 seconds. No less, no more. Specifications. – MLProgrammer-CiM Dec 02 '12 at 02:51
  • Background Agent cannot be started every 1 minute. All you can do is use the once per 30 min execution or use the Location tracking background execution together with something like http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.95).aspx – Martin Suchan Dec 02 '12 at 16:31
  • @EfEs Yes you can. The geolocation is not considered to be a periodictask so you can have both. – José Leal Apr 26 '13 at 09:03
1

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.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166