1

I have a requirement to constantly poll an API and provide notifications in UWP app irrespective of whether it is Visible to user OR minimized OR Not in focus OR Suspended.

If I choose to write it in the app itself, it stopped working while app is suspended and resumed while again app is opened or kept in focus. So I chosen Background Task so that it runs everytime even when app is suspended (ex: minimized).

But now any background task is limited to short span of 30secs and if it is triggered using Application Trigger it lasts max for 10min.

I tried re triggering the background task on completion of 10mins, but during the re trigger phase if app is minimized or device is locked it doesn't happen. Re trigger is happening only after I resume the app by which I'm loosing the indefinite behavior.

I also tried adding the restricted capability to run task indefinitely but this is not allowed for Store app and only for Side loading. But My requirement is to support this for Store app.

Kindly help me how to achieve this behavior using Background Task, or else any other model available in UWP platform to achieve this.

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
SiD
  • 109
  • 1
  • 8
  • http://idownvotedbecau.se/nomcve/ .. Please add related code on how you are registering/de-registering the background task..Also,please go through https://learn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task .. – Pratyay Apr 16 '18 at 13:24
  • I'm using a In-proc background task. The ref you provided is out of proc background task. But diff b/w these is only the process wise, but the background task as such behaves in same way and performs similar traits. Correct me if I'm wrong. How to add attachment here to submit the code? – SiD Apr 17 '18 at 05:09
  • I have removed my downvote as this seems to be a valid question. For adding code you can simply edit your question and paste it there, and format it with the code formatter present in the text toolbox. – Pratyay Apr 17 '18 at 05:40
  • Also how often do you need to make the API call ? if it is every 10-15mins or so, I would recommend going for the out of process execution as store apps are have some restrictions when suspended. – Pratyay Apr 17 '18 at 05:42
  • As per our requirements it should be any value in a range of (10sec - 1 min). So does out of proc background task runs indefinitely with application trigger instead of 10min restriction? Could you elaborate on what are the restrictions for store apps when suspended? – SiD Apr 17 '18 at 08:56

1 Answers1

2

To run background tasks for extended/indefinite periods of time you need to declare the extendedBackgroundTaskTime capability in your app's manifest.

https://learn.microsoft.com/en-us/windows/uwp/launch-resume/run-in-the-background-indefinetly

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
  • Yes, I tried this and it works. But this is a restricted capability which is not allowed for store submission scenario, but I need it for store app. Ref: https://learn.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations – SiD Apr 17 '18 at 05:01
  • Let me know of any other possible solution to this problem – SiD Apr 17 '18 at 05:07
  • It's restricted by design. Store apps shouldn't be using the system's CPU/battery resources without bounds. What is your scenario? – Stefan Wick MSFT Apr 17 '18 at 05:16
  • We have a requirement to monitor a printer continously based on a polling rate (10sec - 1 min) configured in the application and provide notfications on configured events (Ex: Paper empty, Job Done). So we need to support the polling (querying a web service API) even when app is minimized, so I chose Background Task path which is designed to run even when minimized but now I landed with time constraint – SiD Apr 17 '18 at 08:53
  • When the app is minimized it can keep running indefinitely by using an ExtendedExecutionSession, instead of a background task. Have you looked at that? Btw, is this an LOB/Enterprise scenario? LOB apps can use all restricted capabilities. – Stefan Wick MSFT Apr 17 '18 at 13:16
  • Reg your ExtendedExecutionSession, yes I tried that and it's working without any time restriction. Thanks for that. – SiD Apr 18 '18 at 04:58
  • I'm surprised now, actually we had just started developing from scratch and no one in the org knows about this platform. Could you please help me understand the differences b/w LOB and Enterprise scenarios? So that I can check within my org to understand and match their requirement to one of these. – SiD Apr 18 '18 at 04:59
  • If you submit the app to the private catalog for your company it's considered an LOB/Enterprise app and you can use all restricted capabilities. If you submit to the public catalog for general consumers then you won't be able to use restricted capabilities without getting special approval. – Stefan Wick MSFT Apr 18 '18 at 05:12
  • So with this ExtendedExecution paradigm, is there a guarantee that it never gets revoked by the system/OS? Because In background task I had that 10min restriction after which system/OS invokes cancel event on that task. Because my requirement is to indefinitely poll until user closes (not minimizing, currently running while minimized) the app. – SiD Apr 19 '18 at 06:12
  • 1
    On a PC (with AC power) you should see EE get revoked only when the app gets serviced with an update. On a laptop/tablet that runs on battery the EE session will get revoked after 10min. However, you can prevent this by setting the app to "always allowed in the background" in the battery saver settings. – Stefan Wick MSFT Apr 19 '18 at 15:17
  • Could you please reply to this thread also, It's very critical for me. https://stackoverflow.com/questions/49994930/retrieve-ip-address-of-the-default-printer-driver-in-uwp – SiD Apr 24 '18 at 06:48
  • Could you please reply to this thread also, It's very critical for me. https://stackoverflow.com/questions/49994930/retrieve-ip-address-of-the-default-printer-driver-in-uwp – SiD Apr 26 '18 at 07:02
  • That one is not exactly my area of expertise, but I have asked the print folks to take a look and provide an answer. – Stefan Wick MSFT Apr 26 '18 at 13:38
  • That's fine.. As I had told earlier I do a polling task that runs indefinitely in the EE on PC with AC power, I see memory bumping up around 500MB in a day. I see some memory management API's in UWP, is that the ONLY resort to fix this? Is GC collect using Mem Mngr API's a good way in fixing this scenario? Please suggest – SiD Apr 27 '18 at 04:43
  • Could you please address my above comment as well as could you remind your print folks to look up on my other SO thread above. These are little critical for us, kindly provide suggestion. – SiD May 02 '18 at 04:45
  • I would suggest you open a new question about the memory usage, and include some more details, like what you the memory profiler reports, whether GC.Collect reclaims the memory, etc. With the info provided here it's not actionable. It could be a leak anywhere in app, framework or OS code. If it's a leak, no memory mgmt API will help, it needs to be fixed. I will ping the print folks again about the other thread. – Stefan Wick MSFT May 02 '18 at 05:18