Using Extended Execution in Windows 10 Universal Apps

Using Extended Execution in Windows 10 Universal Apps

Windows 10 Universal App now supports extended execution,  which mean you can send a request to operating system to extend the time frame to complete the pending taks before Operating system terminate the app. This post you will learn how to request Extended Execution for the Universal Windows App.

In general applications can be in either one of the three states

  • Not Running
  • Running
  • Suspended

So whenever the app gets changes form one state to another state it receives an events. So the user can do the required operations during that particular state change.

For an instance, when an app is launched it moves form Not Running state to Running State. During this application lifecycle, If user opens some other app or  even minimize the current app then the current app is moved from Running to Suspended state; and this triggers the App Suspended Event.  If there is any app specific data we want to save, this is the right event for the same. By this way, we can re-launch the app from the same place from where it was left out.

Sometimes suspending event gets triggered  just because you have minimized the app for a time interval, even when the system has enough resource. During this time,  If we need to save some data in the server or cloud then it becomes difficult to save because of lack of time provided to the suspending event.
There is no reason why can’t we let the app run little bit longer, where the user can save the data to the cloud, so in windows 10 for Windows Universal App a new concept was introduced called as Extended Execution.

It allows your application to ask for more time to not be suspended.

Whenever our App goes to suspended state then the user can request for extended execution. So if the resource is available the system provided extra time where the user can still do the work in suspended state. In this case the system is running out of resource then a revoke message is sent by the system to the app. so the app has to clean up the task it was doing.

Here is a sample code snippet.


private async void OnSuspending(object sender, SuspendingEventArgs e)

{

var deferral = e.SuspendingOperation.GetDeferral();

using (var session = new ExtendedExecutionSession())

{

session.Reason = ExtendedExecutionReason.Savingdata();

session.Description = "Saving Data to cloud";

session.Revoked += ExtensionRevoked;

var result = await session.RequestExtensionAsync();

if(result == ExtensionExecutionResult.Allowed)

{

await SavingDataToCloud();

}

}

deferral.Complete();

}

 

 Diagram Credits : Abhijit Jana
A Reason and Description has to be provided for the Extended Execution, so the user can see why the application is still using the resource.

Revoke event get triggered when the system is running out of resource and it wants the app to stop using the resource.

RequestExtensionAsync provides the result weather the request is allowed or denied.


private void ExtensionRevoked (ExtendedExecutionSession sender, ExtensionRevokedEventArgs args)

{

//Perform Operations to clode the resource.

}

The biggest advantage of this Extended Execution is when the Application needs to track your location when it is in suspended mode then, it can request for resource and then use it.

Arun Kumar

Arun kumar Surya Prakash, is a  Developer  Consultant. He  is a Microsoft Certified Solution  Developer  for Store App Development, and a Azure Solution Developer. His core skills is on developing Windows Store App and Cross Platform App development. You can follow him @arunnov04

2 Comments to “Using Extended Execution in Windows 10 Universal Apps”

Comments are closed.