March 23, 2018

BUILD 2015 Summary: Application Lifecycle




This BUILD session is about Universal Windows Application lifecycle. What action should be taken when app enters certain state. Some new concepts are also touch in this video below: Background task and extended execution.  I recommend to watch the video, if you can’t, my notes below serves as a summary.

>> Foreground Application Lifecycle

Apps in 1 of 3 states
not running -> running <-> suspended -> not running

>> In VS 2015, there is lifecycle events button, you suspend, resume, and suspend and shutdown

Your code will get notice when app suspended, or resumed by subscribe the events.

>> How to process resume event? Usually you need save app state, user data, because a suspended app can shutdown by the OS, so you may not have chance to the save data then.

Usually, suspend action shouldn’t take more than 5 sec (?), if there is too much work in suspended event handler, Windows will kill it.

>> Extended Execution: ask the Windows your app needs more time before get suspended.
(Please watch video 10:00)

>> There is second form of Extended Execution, where your app is running on foreground, but about to start a  background task, like turn by turn navigation in map app: when user click go, app enter a ExtendedExecutionSession, your can either finish work within the session or get invoked. You have chance to process both.

This is best effort action, it is not guaranteed, depending on machine resource at the time. Your app can get kicked out of execution before you are done.

>> Background Execution
Tracking location, show info through notification, playing music.

>> Trigger based Background Tasks
Apps subscribes to triggers, when trigger is fired, app start to run.

TimerTrigger, SystemTrigger, there are many triggers you can subscribe. (30:00 for list)

>> Resource Management

Default background task has a memory cap 16M, 30sec run time, guaranteed 10% CPU, block in battery-saving mode. long running background task can run forever.

>>In-Proc Background Tasks
By default, background task and foreground task can run in separate process. Their life time are not related.

But you can choose to host a background task in side of foreground task. They are in same process. This make communication between foreground and background tasks much easier. Disadvantage is you have to keep the foreground process alive during the time.

>> Two important triggers

>> Application Trigger
triggered by your app.

Useful for invoking some background task, then foreground task can close, like syncing data to the cloud.

>> Socket Trigger
Register a task, that is triggered by Socket trigger, this task is background task. then foreground task can go away. When socket trigger fires, the background task is waved up.

Useful for notification.