uwpcaliburn.microsuspend

ExtendedExecutionForegroundSession : UWP :: I was panicked


With UWP application ( SideLoaded only ), I develop a monitoring application with Serial port.

Example. I read temperature from sensor via Serial Port. I want to do monitor for 365 days, 24 hours. The application MUST work for all time. No suspending.

6 month

for the development, I spend 6 month .. but My application was stop when minimized. Wow. Really ? I was panicked.... I need your help.

in Debugging mode, All worked fine. Even if minimized, All background task,await task is working without stopping. but Release mode = Sideloaded application was stopped , Especially device read/write.

I added below code to avoid this. but This did not resolve the issue.

// This is Caliburn.micro , UWP framework.
protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        var newSession = new ExtendedExecutionForegroundSession();
        newSession.Reason = ExtendedExecutionForegroundReason.Unconstrained;
        newSession.Description = "Long Running Processing";
        newSession.Revoked += SessionRevoked;
        var result = await newSession.RequestExtensionAsync();
        switch (result)
        {
            case ExtendedExecutionForegroundResult.Allowed:
                    Debug.WriteLine($"ExtendedExecutionForegroundResult.Allowed");
                break;

            default:
            case ExtendedExecutionForegroundResult.Denied:
                Debug.WriteLine($"ExtendedExecutionForegroundResult.Denied");
                break;
        }


private void SessionRevoked(object sender, ExtendedExecutionForegroundRevokedEventArgs args)
    {
        Debug.WriteLine($"ExtendedExecutionForegroundResult :: SessionRevoked");
    }

and, Package.appxmanifest is here.

<?xml version="1.0" encoding="utf-8"?>
<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"
         xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
         IgnorableNamespaces="uap mp rescap">

.....

    <Capabilities>
        <Capability Name="internetClient"/>
        <rescap:Capability Name="extendedBackgroundTaskTime"/>
        <rescap:Capability Name="extendedExecutionUnconstrained"/>
        <DeviceCapability Name="serialcommunication">
            <Device Id="any">
                <Function Type="name:serialPort"/>
            </Device>
        </DeviceCapability>
    </Capabilities>
</Package>

Here is other information.

Please help !

update 1:

I tried to use this code..but same. Not work...

var extendedExecutionSession = new ExtendedExecutionSession();
        extendedExecutionSession.Reason = ExtendedExecutionReason.Unspecified;
        var extendedExecutionResult = await extendedExecutionSession.RequestExtensionAsync();
        if (extendedExecutionResult != ExtendedExecutionResult.Allowed)
        {
            //extended execution session revoked
            extendedExecutionSession.Dispose();
            extendedExecutionSession = null;
        }

Which should I use ?


Solution

  • When running with debugger attached, minimized app doesn't get suspended until manually sent to this state from VS using this button Suspend button. So, there's a chance that all this time, while you were testing the app in the debug mode, it could have worked properly while minimized even with no extended execution permission.

    Consequently, the first thing you'd want to make sure of is that your app is actually allowed to start an extended execution session. I see that you use Debug.WriteLine() to output the result of extended execution session request, but this is not going to work in release mode. Unless you want to use some 3rd party solution (since there's no System.Diagnostics.Trace for UWP) I recommend sending toasts:

        protected override async void OnLaunched(LaunchActivatedEventArgs e)
        {
            ...
            var result = await newSession.RequestExtensionAsync();
            switch (result)
            {
                case ExtendedExecutionForegroundResult.Allowed:
                    ShowToast("Extended execution session request: allowed");
                    break;
    
                default:
                case ExtendedExecutionForegroundResult.Denied:
                    ShowToast("Extended execution session request: denied");
                    break;
            }
            ...
        }
    
        private void SessionRevoked(object sender, ExtendedExecutionForegroundRevokedEventArgs args)
        {
            ShowToast("Extended execution session revoked: " + args.Reason.ToString());
        }
    
        private void ShowToast(string text)
        {
            var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
            var toastTextElements = toastXml.GetElementsByTagName("text");
            toastTextElements[0].InnerText = text;
            ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
        }
    
        // small delay in suspended deferral is needed to buy enough time to send toast from the revoked event
        private async void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            await Task.Delay(3000);
            deferral.Complete();
        }
    

    Please, update your question with the results of checking for extended execution session request in release mode without debugger and provide detailed info about its revocation.

    Use ExtendedExecutionForegroundSession.