3-626.pptx

App Lifecycle: From Activation and Suspension to Background Execution and Multitasking in Universal Windows Apps

1.0x

3-626.pptx

Created 2 years ago

Duration 0:00:00
lesson view count 204
App Lifecycle: From Activation and Suspension to Background Execution and Multitasking in Universal Windows Apps
Select the file type you wish to download
Slide Content
  1. Shawn Henry

    Slide 1 - Shawn Henry

    • Program Manager
    • Windows Developer Ecosystem Platform
    • Windows App Lifecycle  From Activation & Suspension to Background Execution and Multitasking
    • 3-626
    • //build/ content is being presented by Microsoft Office Mix The video for this session will be available shortly
  2. Slide 2

    • Tony
    • 684
    • User Data
    • App Services
    • Arun
    • 765
    • Thomas
    • 762
    • Shell
    • Kernel
    • Shawn
    • 626
    • Title: Windows 10 App Lifecycle: From Activation & Suspension to Background Execution and Multitasking
    • Code: 626
    • Speaker: Shawn Henry
    • Room: TBD
    • Date: 4/29
    • Time: 2.00
    • App
    • Enterprise
    • John
    • Ignite
    • Store
    • Barclay
    • 695
    • Andrew
    • 617
    • App Model
    • Application Model Map - Andrew
    • Roberth
    • 779
  3. How Windows interacts with you app

    Slide 3 - How Windows interacts with you app

    • Suspend and resume
    • Background execution
    • Resource management
    • System triggers and notifications
    • Application Lifecycle
  4. Slide 4

    • ONE WINDOWS
    • Phone
    • Desktops & All-in-Ones
    • Classic Laptop
    • Xbox
    • Tablet
  5. Application Lifecycle

    Slide 5 - Application Lifecycle

  6. Application Lifetime

    Slide 6 - Application Lifetime

    • Apps can be in 1 of 3 states
    • Not Running
    • Running
    • Suspended
    • Application receive events when transitioning between states
    • Except: Suspended->NotRunning
  7. Application Lifetime

    Slide 7 - Application Lifetime

    • Running
    • Suspended
    • Running
    • Suspended
    • Not Running
    • Activated
    • Suspended
    • Activated
    • Suspended
    • Memory
  8. DEMOApplication Lifecycle

    Slide 8 - DEMOApplication Lifecycle

  9. … but what if I need more time?

    Slide 9 - … but what if I need more time?

  10. Continue a session when not in the foreground

    Slide 10 - Continue a session when not in the foreground

    • Location Tracking
    • Save critical data
    • You just want more time
    • Extended Execution
  11. Extended Execution (part 1)

    Slide 11 - Extended Execution (part 1)

    • Running
    • Extension
    • Suspended
    • Request
    • Revoked
    • Memory
    • Suspended
  12. DEMOExtended Execution

    Slide 12 - DEMOExtended Execution

  13. Code Sample

    Slide 13 - Code Sample

    • private async void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); using (var session = new ExtendedExecutionSession()) { session.Reason = ExtendedExecutionReason.SavingData; session.Description = "Upload Data"; session.Revoked += session_Revoked; var result = await session.RequestExtensionAsync(); if (result == ExtendedExecutionResult.Denied) { UploadBasicData(); } // Upload Data var completionTime = await UploadDataAsync(session); } deferral.Complete(); }
  14. Extended Execution (part 2)

    Slide 14 - Extended Execution (part 2)

    • Running
    • Extension
    • NavigatedAway
    • Request
    • Revoked
    • Memory
    • Not Running
    • Suspend
  15. DEMOExtended Execution

    Slide 15 - DEMOExtended Execution

  16. Code Sample

    Slide 16 - Code Sample

    • private async void StartTbTNavigationSession(){ using (var session = new ExtendedExecutionSession()) { session.Reason = ExtendedExecutionReason.LocationTracking; session.Description = "Turn By Turn Navigation"; session.Revoked += session_Revoked; var result = await session.RequestExtensionAsync(); if (result == ExtendedExecutionResult.Denied { ShowUserWarning("Background location tracking not available"); } // Do Navigation var completionTime = await DoNavigationSessionAsync(session); }}
  17. Applications must supply a reason

    Slide 17 - Applications must supply a reason

    • Revoked events will have ~1 sec to clean up
    • Min time is guaranteed but opportunistic afterwards
    • Extended Execution
  18. Background Execution

    Slide 18 - Background Execution

  19. Apps provide real-time content even when suspended

    Slide 19 - Apps provide real-time content even when suspended

    • Background Execution
    • Draw users into your app
    • Delight them with features
  20. Where did we come from?

    Slide 20 - Where did we come from?

  21. Slide 21

    • PLATFORM CONVERGENCE HAS BEEN A JOURNEY
    • Windows Phone 7.5
    • Windows Phone 8
    • Windows Phone 8.1
    • Windows 8
    • Xbox One
    • Windows on Devices
    • Windows 8.1
    • Windows 10
    • Converged
    • background model
  22. Trigger based Background Tasks

    Slide 22 - Trigger based Background Tasks

    • Apps subscribe to triggers they are interested inOnly run *when* trigger is fired
    • Example: Push notification
    • Geofencing BLE device
    • Timer Sensors
  23. DEMOBackground Tasks

    Slide 23 - DEMOBackground Tasks

  24. private void RegisterBackgroundTasks(){    BackgroundTaskBuilder builder = new BackgroundTaskBuilder();     builder.Name = "Background Test Class";     builder.TaskEntryPoint = "BackgroundTaskLibrary.TestClass";     IBackgroundTrigger trigger = new TimeTrigger(30, true);    builder.SetTrigger(trigger);    IBackgroundCondition condition = new 	SystemCondition(SystemConditionType.InternetAvailable);    builder.AddCondition(condition);    IBackgroundTaskRegistration task = builder.Register();    task.Progress += new BackgroundTaskProgressEventHandler(task_Progress);    task.Completed += new BackgroundTaskCompletedEventHandler(task_Completed);}

    Slide 24 - private void RegisterBackgroundTasks(){ BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = "Background Test Class"; builder.TaskEntryPoint = "BackgroundTaskLibrary.TestClass"; IBackgroundTrigger trigger = new TimeTrigger(30, true); builder.SetTrigger(trigger); IBackgroundCondition condition = new SystemCondition(SystemConditionType.InternetAvailable); builder.AddCondition(condition); IBackgroundTaskRegistration task = builder.Register(); task.Progress += new BackgroundTaskProgressEventHandler(task_Progress); task.Completed += new BackgroundTaskCompletedEventHandler(task_Completed);}

    • Registering a background task
  25.     public sealed class TestClass:IBackgroundTask    {        private int globalcount;         void IBackgroundTask.Run(IBackgroundTaskInstance taskInstance)        {            globalcount = 0;             taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);            for (int i = 0; i < 100000; ++i)            {                Interlocked.Increment(ref globalcount);                taskInstance.Progress = (uint)globalcount;             }        }    }

    Slide 25 - public sealed class TestClass:IBackgroundTask { private int globalcount; void IBackgroundTask.Run(IBackgroundTaskInstance taskInstance) { globalcount = 0; taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled); for (int i = 0; i < 100000; ++i) { Interlocked.Increment(ref globalcount); taskInstance.Progress = (uint)globalcount; } } }

    • Background task run implementation
  26. SystemTrigger

    Slide 26 - SystemTrigger

    • TimeTrigger
    • MaintenanceTrigger
    • DeviceUseTrigger
    • DeviceServicingTrigger
    • PushNotificationTrigger
    • CachedFileUpdaterTrigger
    • DeviceConnectionChangeTrigger
    • GattCharacteristicNotificationTrigger
    • RfcommConnectionTrigger
    • LocationTrigger
    • AppointmentStoreNotificationTrigger
    • ContactStoreNotificationTrigger
    • EmailStoreNotificationTrigger
    • BluetoothLEAdvertisementWatcherTrigger
    • BluetoothLEAdvertisementPublisherTrigger
    • DeviceWatcherTrigger
    • ActivitySensorTrigger
    • SensorDataThresholdTrigger
    • ToastNotificationHistoryChangedTrigger
    • ToastNotificationActionTrigger
    • ApplicationTrigger
    • MediaProcessingTrigger
    • SocketActivityTrigger
    • Triggers
    • Windows 8.1
    • WP 8.1
    • Windows 10
  27. Opportunistic Background Tasks

    Slide 27 - Opportunistic Background Tasks

    • Run for as long as resources are available
    • No guarantee of execution
    • ApplicationTrigger, MaintenanceTrigger, DeviceUseTrigger
    • Default Background Tasks
    • Guaranteed minimum execution time of 25 secs
    • Everything else: TimeTrigger, PushTrigger…
    • Example 512MB Mobile device
    • Resource Management
    • Run
    • Cancel
    • Memory
    • Run
    • Task type
    • Memory Cap MB
    • Lifetime
    • CPU
    • Block in Batt-Sav
    • Default Background Task
    • 16
    • 30 sec
    • 10%
    • Y
    • Long-Running
    • 16
    • 10%
    • Y
  28. In-Proc Background Tasks

    Slide 28 - In-Proc Background Tasks

    • App Container
    • app.exe
    • CoreApplication
    • BackgroundTask
    • App Container
    • app.exe
    • CoreApplication
    • BackgroundTask
    • backgroundtaskhost.exe
    • Default Background Task
    • Hosted in a separate process
    • With separate memory caps
    • In Proc Background Task
    • Hosted in the specified process
    • Shares memory caps
  29. Opportunistic task

    Slide 30 - Opportunistic task

    • Triggered from the foreground application
    • Useful for initial syncing of data
    • Application Trigger
  30. DEMOApplication Trigger

    Slide 31 - DEMOApplication Trigger

  31. Apps can have network connectivity and discoverability maintained while suspended or even terminated.

    Slide 32 - Apps can have network connectivity and discoverability maintained while suspended or even terminated.

    • Alternative to WNS triggered Tasks
    • How it works:
    • In foreground app process:
    • Create/retrieve socket(s)
    • Register background task to be woken up
    • Give socket to broker service as app is suspending
    • Take socket back from broker service at any time
    • On incoming network activity on brokered socket:
    • Background task is triggered
    • Take socket back from broker service
    • Socket Trigger Tasks
  32. private void registerBGTask(){

    Slide 33 - private void registerBGTask(){

    • var socketTaskBuilder = new BackgroundTaskBuilder();
    • socketTaskBuilder.Name = _backgroundTaskName;
    • socketTaskBuilder.TaskEntryPoint = _backgroundTaskEntryPoint;
    • var trigger = new SocketActivityTrigger();
    • socketTaskBuilder.SetTrigger(trigger);
    • _task = socketTaskBuilder.Register();
    • }
    • private void TransferOwnership(StreamSocketListener tcpListener){
    • await tcpListener.CancelIOAsync();
    • var dataWriter = new DataWriter();
    • String context_to_save = “Current state is 1”;
    • dataWriter.WriteInt32(context_to_save);
    • var context = new SocketActivityContext(dataWriter.DetachBuffer());
    • tcpListener.TransferOwnership(_socketId, context)
    • }
    •  private void OnSuspending(object sender, SuspendingEventArgs e){
    • var deferral = e.SuspendingOperation.GetDeferral();
    • TransferOwnership(_tcpListener);
    • deferral.Complete();
    • }
    • public async void Run(IBackgroundTaskInstance taskInstance){
    • var socketActivityDetails = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
    • var reason = socketActivityDetails.Reason;
    • if (reason == SocketActivityTriggerReason.ConnectionAccepted){
    • var connectedSocket = socketActivityDetails.SocketInformation.StreamSocketListener;              
    • var reader = new DataReader(socketActivityDetails.SocketInformation.Context.Data.AsStream().AsInputStream());
    • var restored_context = reader.ReadInt32();
    • // Do stuff with the socket
    • }
    • }
    • Socket Broker APIs – Sample Code
  33. DEMOSocket Trigger

    Slide 34 - DEMOSocket Trigger

  34. Cancellation of long running tasks

    Slide 35 - Cancellation of long running tasks

    • Application trigger
    • Things not in today’s build
  35. Color back option.

    Slide 36 - Color back option.

    • Use the Format Shape menu to change the color of the shape.
    • This text box will automatically center to align beside the color shape.
    • Things coming in 2015
  36. Slide 37

    • Wrap up
    • 1. Background processing is converged with Windows2. There are lots of new things you can do3. But resources are still limited