maloo
maloo

Reputation: 825

Is it possible to prevent suspend in LOB UWP desktop application/background task?

I have been tasked at porting a LOB desktop app from .NET 4.5.2 to Windows 10 UWP. In normal Win32 apps an app runs until the user closes the app (service and application). But when reading up on UWP apps all docs I find say that there is no way to prevent an app from suspending (only temporary or until quota exceeded). I understand this on Mobile/Tablet, but not on a desktop machine with performance and power available.

A common answer to this problem is to use a "real" server (usually a web server). But our app is used in the field where internet is not available. So we need the server/service to run on a desktop machine.

The best option so far I found is a UWP app with built in background task asking for deferral. But as I understand this still offer no guarantees regarding suspend. Only some vague info that "you probably don't get suspended since it is a desktop machine with lots of resources in the global pool" ...

And I don't like to keep the LOB service app in .NET and clients in UWP since they share a lot of code that can't be reused between .NET and UWP.

So, anyone that got a definite answer/reference to wether it is possible to prevent suspend for service style UWP app/task running on Win10 desktop machine?

Upvotes: 4

Views: 3577

Answers (3)

chcortes
chcortes

Reputation: 116

Update: Enterprise-specific capabilities have been created for LOB apps to run as long as needed, here is information on those: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/run-in-the-background-indefinetly

The answer in the UserVoice from Clint is for all apps targeting the consumer Microsoft Store:

In Windows 10 Anniversary Update (Build 14393) we updated Extended Execution Unspecified Reason to be battery aware for the PC device family. It is requested in the foreground and if the device is connected to AC power or if it has a battery but the user has set “Always Allowed” in the battery use settings then that app will be able to run without a time limit. This works with the single process model as Extended execution does not work with a background task. BackgroundMediaPlayback capability also won’t be suspended for certain scenarios. We are keeping this open as there is more we can do. If you have scenarios we don’t work well in with the updated models, please tell us!

So the foreground app can keep from suspending using Extended Execution Unspecified with battery settings, Extended Execution Location Tracking, or the BackgroundMediaPlayback capability depending on the scenario you need. From your description it sounds like the Unspecified Extended Execution option would be the most relevant option. Here is a link to the sample code: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ExtendedExecution

Upvotes: 2

Sean Leith
Sean Leith

Reputation: 1

I think your best bet would be something called App Services, which is new for Windows 10. From Microsoft MVA: http://www.microsoftvirtualacademy.com/training-courses/a-developers-guide-to-windows-10, note: this link is a little outdated, but information is still valid. You may want to find the updated version of same content.

Basically, App services are like web services, that can be consumed by other apps. Pay attention of its life cycle. It appears to me that, its life cycle is similar to a Windows service, but double check on that.

Upvotes: 0

Alan Yao - MSFT
Alan Yao - MSFT

Reputation: 3304

A similar uservoice has been raised on wpdev.uservoice.com where Microsoft is listening suggestions. You are highly encouraged to vote on it, add your comments and monitor it.

I believe preventing suspend or not should be decided by user, but there is no way to do that. And I agree, in some special scenarios, it makes sense.

But currently the Application lifecycle for UWP is very clear about the suspended state. On Desktop family, UWP apps suspend when they are minimized or when Windows enters a low power state.

[Update]

One more possible solution you may not know is a new feature introduced in Windows 10 - Extended execution session.

Currently there is no official sample about this feature, but you can check Jerry Nixon and Andy Wigley’s MVA session about application lifecycle(#13). And here is Q&A record for that session where you can find a sample code. You can download the ppt included in the MVA session which includes the following code for your quick reference:

private async void OnSuspending(object sender, SuspendingEventArgs args)
{
    var deferral = e.SuspendingOperation.GetDeferral(); using (var session = new ExtendedExecutionSession { Reason = ExtendedExecutionReason.SavingData })
    {
        session.Description = "Upload Data";
        session.Revoked += (s, e) => { Log("Save incomplete"); }; try
        {
            if (await session.RequestExtensionAsync() == ExtendedExecutionResult.Denied)                 // takes 3 seconds
                UploadBasicData();
            else                 // takes 8 seconds
                await UploadDataAsync(session); Log("Save complete");
        }
        catch { Log("Save failed"); }
        finally { deferral.Complete(); }
    }
}

But as Jerry mentioned, there is still no guarantee it is 100% reliable.

Upvotes: 5

Related Questions