150 likes | 168 Views
Learn about multitasking in the Fall Creators Update with the single process model and persistent events. Explore background media playback, location tracking, running while minimized, and restricted scenarios for enterprise.
E N D
Windows Developer Day Fall Creators Update • Chris Cortes • UWP Program Manager • October 10, 2017
Multitasking in the Fall Creators Update Single Process Model and Persistent Events • Chris Cortes • UWP Program Manager
Multitasking in UWP • Single Process and Multiple Process Models • Multitasking for your Scenario • Persistent Events
Single Process Model • Activate in Background • Launch/Activated • Background Activated • New Callbacks • Leaving Background • Entered Background • Suspended • Terminated • Foreground Resume • Background Resume
Background Media Playback • Add Manifest Entry • Use any Windows Audio API • Stream to hold Network • <Capabilities> • <uap3:CapabilityName="backgroundMediaPlayback“ /> • </Capabilities> • var player = newMediaPlayer(); • player.Source = MediaSource.CreateFromUri(newUri("https://contoso.com/song.mp3")); • player.Play();
Background Location Tracking • Extended Execution Reason • Check Background Access • Request in Foreground • privateasyncvoidStartNavigation(){ • session = newExtendedExecutionSession(); • session.Reason = ExtendedExecutionReason.LocationTracking; • session.Description = “Turn by Turn Navigation"; • session.Revoked += session_Revoked; • var result = awaitsession.RequestExtensionAsync(); • if (result == ExtendedExecutionResult.Allowed){ • StartPeriodicLocationUpdate(); • } • else { CheckBackgroundAccessAndNotifyUser(); } • } • privateasyncvoidsession_Revoked(object sender, ExtendedExecutionRevokedEventArgsargs){ • if(args.Reason == ExtendedExecutionRevokedReason.SystemPolicy){ • NotifyUser(); • } • }
Run While Minimized • Extended Execution Reason • Handle Request Result • Handle Revoked Event • Battery-Aware • privateasyncvoidRequestBackgroundProjectCompilation(){ • session = newExtendedExecutionSession(); • session.Reason = ExtendedExecutionReason.Unspecified; • session.Description = “Compile Project File"; • session.Revoked += session_Revoked; • var result = awaitsession.RequestExtensionAsync(); • if (result == ExtendedExecutionResult.Allowed){ • StartCompilation(); • } • else { CheckBackgroundAccessAndNotifyUser(); } • } • privateasyncvoidsession_Revoked(object sender, ExtendedExecutionRevokedEventArgsargs){ • if(args.Reason == ExtendedExecutionRevokedReason.SystemPolicy){ • NotifyUser(); • } • }
Restricted Scenarios for Enterprise • ExtendedBackgroundTaskTime • No Execution Time Exceeded on Background Tasks • Background Access and Battery Saver still apply • ExtendedExecutionUnconstrained • ExtendedExecutionForegroundSession • Run while Minimized until Standby
Background Activation with Triggers • New Triggers and Capabilities Added Each Release • Multi-Instance • Resource Grouping • Task Grouping • Background Activated • Overridden Method in XAML • Event on CoreApplication • Background Access Status • User Controls in Settings • Battery Saver
Persistent Event Creation • . • Background Activation for Frameworks • Separate Registrations from AllTasks • Background Task Registration Group Event • Create Event Registration Token Table conststringMyTaskGroupId = "4F8904E0-3F25-9A0C-41D3-0305E82C3333"; conststringMyTaskGroupDebugName = "My Task Group"; var group = BackgroundTaskRegistration.GetTaskGroup(MyTaskGroupId); if (group == null) { group = newBackgroundTaskRegistrationGroup(MyTaskGroupId, MyTaskGroupDebugName); } var builder = newBackgroundTaskBuilder(); builder.Name = "Tasks Name"; builder.IsNetworkRequested = true; builder.TaskEntryPoint = "Tasks.MyTask"; builder.SetTrigger(myTrigger)); builder.TaskGroup = group; builder.Register();
Persistent Event Usage • Background Activation for Frameworks • App Subscribes to Event • App Unsubscribes to Unregister • public App(){ • this.InitializeComponent(); • this.Suspending += OnSuspending; • PersistentEvents.MaintenanceWindow += BgWork.Maintenance; • } • publicstaticvoid Maintenance(object sender, PersistentEventArgs e){ • vardef = e.GetDeferral(); • UpdateUserContent(); • def.Complete(); • }
Multitasking with the Single Process Model • Background Media Playback • Location Tracking • Run While Minimized • Restricted Scenarios for Enterprise • Background Activation • Persistent Events
Additional Information • Single Process Model: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle • Background Activation: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-an-inproc-background-task • Background Task Registration Groups: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/group-background-tasks • Manifest Attributes for Background Tasks: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/declare-background-tasks-in-the-application-manifest
Code Samples • Background Media Playback: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundMediaPlayback • Extended Execution: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ExtendedExecution • Background Activation: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundActivation • Persistent Events: https://github.com/cc-cortes/Simple-Persistent-Events