5
votes

My background task takes a long time to complete, and the OS is just killing it. I'm trying to sync my contacts online, here's what I'm doing:

  1. Get all contacts from phonebook (takes ~1 second)

  2. Upload them to a server (~2 seconds)

  3. Retrieve all contacts from server (~2-3 seconds)

  4. Delete all contacts from ContactStore(ContactStore.DeleteAsync sometimes takes 1 minute to complete)

  5. Create a ContactStore and import all contacts )(~1-2 minutes for 1000 contacts)

I have ~100 contacts and it's working well, but I wanted to test with ~1000 contacts and it doesn't complete every time. I'm using a MaintenanceTrigger, but I think it's still too much for a background task, but I need a confirmation for this. MaintenanceTrigger tasks should be allowed to do more resource intensive tasks, so why is the OS killing my background task?

3
Really hard to answer this without seeing some code. The thing that comes to mind is maybe your background task is using too much cpu, memory, or taking to long to complete so the phone is killing itKen Tucker
If I'm using a maintenance trigger, should the task be allowed to complete even if it takes a lot of time? The phone is charging so it shouldn't be a problem if it's an resource intensive taskbogdanbujdea
@thewindev please edit your question to make it clear that it isn't 1-2 mins for 1000 contacts... but 12mins as you later pointed out... your question is misleading and confusing for future readers.Paul Zahra
actually, in some cases it took 2-3 minutes, in others it took 12 minutesbogdanbujdea

3 Answers

3
votes

Take a look at this link: https://msdn.microsoft.com/en-us/library/windows/apps/hh202942(v=vs.105).aspx

Resource intensive tasks are constrained to a duration of 10 minutes.

The following constraints must be met before the task is started. If the device stop fulfilling these constraints the agent is terminated immediately.

  • External power required
  • Non-cellular connection required
  • Minimum battery power
  • Device screen lock required
  • No active phone call
  • Cannot change network to cellular

Besides this there are also a memory cap of respectively 11mb and 20mb for low/high end devices.

From your description above the most likely scenario IMO is the memory cap being hit. Maybe this post can help you look into the memory usage of your background task: How to get memory available or used in C#

Key changes to memory limits starting in Windows Phone 8.1 include (found here):

  • All Windows Phone 8 foreground apps are treated the same. We no longer have different memory caps for XNA, native or Silverlight apps.
  • Windows Phone 8.1 apps (including both Silverlight 8.1 and Windows Runtime) apps do have slightly higher caps than Windows Phone 8 apps.
  • Memory caps for all app types, including Continuous Background Execution (CBE), scale up with increased memory.
  • There is no longer a "default" and "higher" cap - there is only the default cap.
  • The ID_FUNCCAP_EXTEND_MEM manifest entry is ignored for all apps running on Windows Phone 8.1.
  • The ID_REQ_MEMORY_300 manifest entry is still valid, but you should really make your app run on all devices.
  • The new equivalent of ID_REQ_MEMORY_300 is below. This entry should be added to the AppX manifest (not to the WMAppManifest).
3
votes

Finally, my task got canceled with the reason ExecutionTimeExceeded, so this is the problem. It seems that trying to import ~1000 contacts in the ContactStore takes ~12 minutes, which is too long for a background task. I'll have to make the user to open the app and do the import. Thank you for your help.

1
votes

erm... may be silly but...

"Background tasks that use a maintenance trigger run only when the system is connected to AC power." Taken from MSDN

Could it be plugged into the mains when it works?, and not plugged in when it doesn't work?

EDIT: Are you considering how busy the phone is when you try to 'sync' contacts? Are you forcing the app to allways run in the background via Battery Saver?

You could do something like this to see how busy your phone is... or it could be the battery saver halting your app if download size etc limits are reached...

Taken from here...

var result = await BackgroundExecutionManager.RequestAccessAsync();

if (result == BackgroundAccessStatus.Denied)
{
    // Handle this if it is important for your app.
}

"If the result is denied the phone thinks it has too much background task active. In that case you can prompt your users to go the Battery saver application and force allow your app to run in the background even if the phone don´t want to..."