yasen
yasen

Reputation: 3580

Windows Phone background task stops prematurely

I'm developing a WP8.1 SL app which uses background tasks (not agents!).

My task is triggered by a TimerTrigger - once every 30 minutes (which I believe is the minimum interval for Windows Phone, right?). It is doing a lot of work, and for some reason, sometimes it gets interrupted - that is, it stops in the middle of it. The way I know it's in the middle of it is because I log what's happening and the task's work is basically the same every time.

I deployed the app to my device yesterday to test a new approach, and everything was working fine - the background task was doing everything every time it was started - worked like a charm. Today my device needed a soft reset, so I did it (nothing was working, it happens from time to time since I updated to WP8.1). Since that moment, the background task gets interrupted every time, right in the middle of it, just as with the previous approach.

Any ideas what can cause this? I'm thinking it may be connected to the soft reset, because - 100% success rate before it, 0% after that.

What I've tried so far:

What I'm not sure about:

Any ideas why my background task is sometimes stopped at the middle? I'm really having a hard time determining how to fix/improve the app and will it work at all.

Thanks.

Upvotes: 7

Views: 1557

Answers (1)

yasen
yasen

Reputation: 3580

So, I think I found what my problem was.

First of all, the background task just needed a lot of CPU time, more than the limit, and that's why it was getting killed in the middle.

The reason why it was working sometimes, was because sometimes the device on which I was testing treated the app as being in debug (or with attached debugger). In this case the restriction on CPU time is removed.

So, even if I uninstall the app and rebuild it in Release and redeploy, and start it without the debugger attached, the device was NOT enforcing the constraints. That only happens, if I have started the app with the debugger at least once (or maybe the first time) after it's deployed. Restarting the device (just power off and then turn on) fixes this.

While I was testing, using the same build of an app, its background task ran for 40 MINUTES on one device, and just 3-4 seconds on another. After I restarted the first device, the background task started behaving normally (running for just a few seconds).

So, if you want to test your background task for the CPU constraint:

  1. You must be testing on a device, not on the Emulator.
  2. Uninstall the app, if it's installed.
  3. Restart the device.
  4. Deploy a Release build of the app.
  5. Run the app, so that it can register the background task, and then close it.
  6. Wait for the background task to be invoked. (You could add a trigger that can be forced, like TimeZoneChanged or UserPresent, so that you could quickly test it.)

P.S. This may not be the perfect answer, but these are my observations, and they helped me solve my problem. This by far is the best method I have found for testing the CPU time, and it's far from perfect. So, if anyone has better ideas, please share them.

Upvotes: 13

Related Questions