5

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?

bogdanbujdea
  • 594
  • 8
  • 22
  • 2
    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 it – Ken Tucker Feb 21 '16 at 16:28
  • 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 task – bogdanbujdea Feb 24 '16 at 08:40
  • @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 Mar 03 '16 at 12:23
  • actually, in some cases it took 2-3 minutes, in others it took 12 minutes – bogdanbujdea Mar 04 '16 at 12:15

3 Answers3

3

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).
Community
  • 1
  • 1
Kasper Holdum
  • 12,993
  • 6
  • 45
  • 74
  • My app is for WP 8.1, not WP 8, so I think the memory requirements have increased. Anyway, my task consumes 12 MB maximum, and my device is a Lumia 930 running on Windows 10 latest build. All of those constraints are met. I forgot to say that sometimes the task is finished, and sometimes it's not. – bogdanbujdea Feb 24 '16 at 08:56
  • I'm not sure the memory limit is increased. I could not find any updated documentation that says that anything has changed for Windows Phone 8.1. – Kasper Holdum Feb 24 '16 at 09:07
  • @thewindev "You can check the memory limit available to your app by checking the ApplicationWorkingSetLimit value using the DeviceExtendedProperties.GetValue(String) method. For an example of how to do this, see How to disable features in apps for lower-memory phones for Windows Phone 8." Taken from https://msdn.microsoft.com/en-us/library/windows/apps/jj681682%28v=vs.105%29.aspx – Paul Zahra Feb 24 '16 at 09:22
  • I did this and I use 14 MB/115 MB(the limit). I used MemoryManager to get memory usage and memory limit. So it seems that memory is not an issue – bogdanbujdea Feb 24 '16 at 13:45
  • @KasperHoldum As the accepted answer was actually answered in the link you gave and not in the text of your post... to prevent this arguably being a 'link only' answer (the rest is fluff) could you please extract the answer from the link and edit it into your post. – Paul Zahra Mar 03 '16 at 12:18
3

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.

bogdanbujdea
  • 594
  • 8
  • 22
  • If you had told us it took 12 minutes instead of saying it took max 3 minutes this would have been the obvious cause. The link I give in my answer specifies 10 minutes max as a constraint. – Kasper Holdum Mar 01 '16 at 11:49
  • you are right, I haven't read the entire page because it was for Windows Phone 8 I'll accept your answer and give you the bounty :) – bogdanbujdea Mar 01 '16 at 12:07
1

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..."

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • not silly :) but the task doesn't trigger when it's not on AC power, so this is not the issue – bogdanbujdea Feb 24 '16 at 10:46
  • @thewindev Also this may provide some insight for you https://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61/ – Paul Zahra Feb 24 '16 at 15:28
  • @thewindev You could also try using the Progress event of the IBackgroundTaskRegistration object... As in set it at in the background task and in your app read the event and the value and log it to see if it is always stopping at the same point. – Paul Zahra Feb 24 '16 at 15:32