Running a background thread when minimized

DUCOS Eric (DEXIP) 116 Reputation points
2021-03-18T14:12:54.247+00:00

Hi,

Before telling me to use BackgroundTask component, I want to say that I already think about, but I do not see how to use it without rewriting a whole of things with the risk of breaking other things...

My need is to have the same as what is running good on android platform (whereas I am not an adept of android platform...but in this case, this works on android, but I cannot make working on my preferred Windows Platform !!!)

The need is to get a background thread launched at startup continue to work even if the app is minimzed or put in background.
My thread has to many interaction with some foreground data and services used in the front UI, and I think that I cannot move it to a UWP Background task component...and I am not sure that it could run in infinite in such a way...

I searched on the Web and I thought that I found the solution on this Microsoft documentation page:
https://learn.microsoft.com/en-us/windows/uwp/launch-resume/run-in-the-background-indefinetly

Can someone help me implement that in my Xamarin Form app ?
I tried to follow instructions, but I missed someting because it does not recognize the restricited capability in my manifest when I try to deploy locally my app.

I must say also that the app is NOT deployed in the Microsoft Store. It is manually deployed in a side by side way locally on entreprise devices.

Regards.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points
    2021-03-19T07:47:36.907+00:00

    Hello, Welcome to Micorosoft Q&A,

    Can someone help me implement that in my Xamarin Form app ? I tried to follow instructions, but I missed someting because it does not recognize the restricited capability in my manifest when I try to deploy locally my app.

    The document Run in the background indefinitely is suit for your scenario, for running background tasks indefinitely, you need enable restricited capability

    I will share the complete manifest content that make restricited capability available, please refer the following content to edit your own manifest.

    double click manifest-> press F7 edit with xml-> add the rescap namespace under the package attribute then specific it in the IgnorableNamespaces attribute.

    <Package  
        ...   
        xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"    
        IgnorableNamespaces="uap mp rescap">   
    ...  
      <Capabilities>   
        <rescap:Capability Name="extendedBackgroundTaskTime"/>  
      </Capabilities>  
    </Package>  
    

    Please note

    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 Windows Creators Update (version 1703), the extendedBackgroundTaskTime restricted capability was introduced to remove these limits.

    Please make sure your app's target min version large than 1703.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. DUCOS Eric (DEXIP) 116 Reputation points
    2021-03-19T16:00:29.223+00:00

    Hey guy,

    I do not understand what happened.

    What you described is what I had done before...and I had a problem when deploying...

    But I have done again and now it works (computer mysteries !!!)..or perhaps I did not do exactly the same thing..

    I just had to move the ExtendedExecutionForegroundSession as a global variable in my UWP App class and now, when I minimized my app, the background thread continue to work.

    Thanks for your time.

    Regards.

    I just replace your capabilities with this one that seems to me more appropriate according to the Microsoft documentation: "extendedExecutionUnconstrained"

    0 comments No comments