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.
yasen
source share