Search code examples
c#uwpbackground-task

How to increase the runtime of a background task?


so I have a Background task on UWP that downloads data from a server. This task may take up to a minute depending on internet speeds. I found that when my user minimised the app the download was paused, so decided to run it on a BackgroundTask

When testing I found my download was not finishing and later found from this link that background tasks have a limitation:

Background tasks are limited to 30 seconds of wall-clock usage.

But after some more googling I found this link which speaks of a way to increase the timeout of a background task

In the Universal Windows Platform, background tasks are processes that run in the background without any form of user interface. Background tasks may generally run for a maximum of twenty-five seconds before they are cancelled. Some of the longer-running tasks also have a check to ensure that the background task is not sitting idle or using memory. In the in the Windows Creators Update (version 1703), the extendedBackgroundTaskTime restricted capability was introduced to remove these limits. The extendedBackgroundTaskTime capability is added as a restricted capability in your app's manifest file:

Package.appxmanifest

XML

 <Package ...> 
    <Capabilities>  
        <rescap:Capability Name="extendedBackgroundTaskTime"/>      
    </Capabilities> 
  </Package> 

This capability removes execution time limitations and the idle task watchdog

But after adding the above to my package.appxmanifest file I get the following error:

Content of the file 'Package.appxmanifest' is not well-formed XML. 'rescap' is an undeclared prefix

So is it possible to remove this background task limitation from my app?


Solution

  • As Mike McCaughan observes in the comments on the question, the most likely explanation for the specific error you are seeing is that you simply have not declared the rescap namespace on the enclosing Package element that is being referenced by the capability tag:

    <Package
        xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
        xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
        xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
        xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
        IgnorableNamespaces="uap mp wincap rescap">
    

    Whether you need the other namespaces or not I cannot say, but the key one involved in the restricted capabilities case you are contending with is the one for xmlns:rescap.

    Information taken from the documentation provided by Microsoft here.