Windows Phone background task terminates prematurely

I am developing a WP8.1 SL application that uses background tasks (not agents!).

My task is launched by TimerTrigger - once every 30 minutes (which I consider the minimum interval for Windows Phone, right?). He does a great job, and for some reason he sometimes breaks - that is, he stops in the middle. . I know that it is in the middle, because I enter the log of what is happening, and the work of the task is basically the same every time.

Yesterday, I deployed the application on my device to check the new approach, and everything works fine - the background task did everything every time it was launched - it worked like a charm. Today, my device needed a soft reset, so I did it (nothing worked, it happens from time to time since I upgraded to WP8.1). From this moment, the background task is interrupted every time, right in the middle, as in the previous approach.

Any ideas what might cause this? I think that it can be connected to a soft reset, because - 100% success before it, 0% after that.

What I have tried so far:

  • I register errors, I have an attempt to get around everything, I signed up for the UnobservedException event and the Canceled event, and I register the suspension counting - none of these things help, It seems there are no errors, it is not delayed, and it is not canceled.

  • I occasionally record the current memory usage, which is approximately 16-17 MB. On my device, the limit should be 30 MB, so I don’t think the problem is.

  • I call RequestAccessAsync wherever I can. I thought that someday this would be enough, but with this soft reset of the problem, I decided to put it in 1-2 other places to check if this caused the problem. Well, this is not the case, or at least it cannot be fixed.

What I'm not sure:

  • I have no idea how to check the processor time that my task consumed. I cannot find a good / reliable way to do this. In addition, I can not find any information that would explain why the task sometimes stops in the middle, and sometimes it works fine.

Any ideas why my background task sometimes stops in the middle? It’s really difficult for me to determine how to fix / improve the application, and whether it will work at all.

Thanks.

+7
windows-runtime
source share
1 answer

So, I think I found my problem.

First of all, the background task just took a lot of processor time, more than the limit, and why he was killed in the middle.

The reason why it sometimes worked was because sometimes the device I was testing on saw the application as being in debugging (or with an attached debugger) . In this case, the CPU time limit is removed.

So, even if I delete the application and rebuild it in Release and redeploy, and run it without the debugger attached, the device did NOT apply restrictions. This only happens if I started the application with the debugger at least once (or maybe the first time) after it is deployed. This may cause the device to restart (just turn off the power and then turn it on).

While I tested using the same build of the application, its background task ran for 40 MINUTES on one device and only 3-4 seconds on another. After I restarted the first device, the background task began to behave normally (it only takes a few seconds).

So, if you want to test the background job to limit the CPU:

  • You should test the device, not the emulator.
  • Uninstall the application, if installed.
  • Reboot the device.
  • Expand the application release design.
  • Launch the application so that it can register the background task, and then close it.
  • Wait for the background task to be called. (You can add a trigger that can be forced, such as TimeZoneChanged or UserPresent, so you can quickly test it.)

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

+13
source share

All Articles