ExtendedExecutionSession is not working and uwp app is getting suspended.

Alumni Comp 16 LAXMI SWAMI 46 Reputation points
2021-08-04T09:47:03.547+00:00

I am using BeginExtendedExecution to avoid app suspension but it is getting revoked due to system policy.

Here is my code

 private async void BeginExtendedExecution()
        {         
                ClearExtendedExecution();

                var newSession = new ExtendedExecutionSession();
                newSession.Reason = ExtendedExecutionReason.Unspecified;
                newSession.Description = "Raising periodic toasts";
                newSession.Revoked += SessionRevoked;
                ExtendedExecutionResult result = await newSession.RequestExtensionAsync();

                switch (result)
                {
                    case ExtendedExecutionResult.Allowed:                            
                        session = newSession;
                        break;

                    default:
                    case ExtendedExecutionResult.Denied:
                            newSession.Dispose();
                        break;
                }
        }


 private async void SessionRevoked(object sender, ExtendedExecutionRevokedEventArgs args)
        {
                 await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    switch (args.Reason)
                    {
                        case ExtendedExecutionRevokedReason.Resumed:                       
                            break;

                        case ExtendedExecutionRevokedReason.SystemPolicy:
                            break;
                    }

                    EndExtendedExecution();
                });
        }

        private void ClearExtendedExecution()
        {
            if (session != null)
            {
                session.Revoked -= SessionRevoked;
                session.Dispose();
                session = null;
            }

            if (periodicTimer != null)
            {
                periodicTimer.Dispose();
                periodicTimer = null;
            }
        }

        private void EndExtendedExecution()
        {
            ClearExtendedExecution();
        }

Onsuspending method is getting called after few mins of app getting into background

 private void OnSuspending(object sender, SuspendingEventArgs e)
 {
             var deferral = e.SuspendingOperation.GetDeferral();
                deferral.Complete();
  }
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2021-08-05T07:22:31.253+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Yes, the limitation on the laptop is the reason why you get such behavior.

    So is there anything we can do to avoid this?

    Based on the document: Postpone app suspension with extended execution, it mentions that tablets and laptops could get some long-running behavior. A tablet or laptop user can get the same long running behavior--at the expense of battery life--when the Allow the app to run background tasks option is selected in Battery usage by app settings. (To find this option on a laptop, go to Settings > System > Battery > Battery usage by App (the link under the percent of battery power remaining) > select an app > turn off Managed By Windows > select Allow app to run background tasks.

    You could try the way that the document mentions. If you still have questions, please feel free to contact us.

    Thank you.


    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