Skip to content

Commit

Permalink
Introducing BootStrapper.AutoExtendExecutionSession to close Windows-…
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrynixon committed Sep 22, 2016
1 parent b3ed570 commit 0b82fed
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions Template10 (Library)/Common/BootStrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected sealed override void OnWindowCreated(WindowCreatedEventArgs args)
var window = new WindowWrapper(args.Window);
ViewService.OnWindowCreated();
WindowCreated?.Invoke(this, args);
base.OnWindowCreated(args);
base.OnWindowCreated(args);
}

#region properties
Expand Down Expand Up @@ -226,12 +226,12 @@ from state will fail because of missing values.
This is okay & by design.
*/


if (EnableAutoRestoreAfterTerminated)
{
var launchedEvent = e as ILaunchActivatedEventArgs;

if(DetermineStartCause(e) == AdditionalKinds.Primary || launchedEvent?.TileId == "")
if (DetermineStartCause(e) == AdditionalKinds.Primary || launchedEvent?.TileId == "")
{
restored = await NavigationService.RestoreSavedNavigationAsync();
DebugWrite($"{nameof(restored)}:{restored}", caller: nameof(NavigationService.RestoreSavedNavigationAsync));
Expand Down Expand Up @@ -688,17 +688,16 @@ private async void HandleResuming(object sender, object e)
}
}

private async void HandleSuspending(object sender, SuspendingEventArgs e)
public bool AutoExtendExecutionSession { get; set; } = true;

private void HandleSuspending(object sender, SuspendingEventArgs e)
{
DebugWrite();

// one, global deferral
var deferral = e.SuspendingOperation.GetDeferral();
using (var session = new Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionSession
{
Description = this.GetType().ToString(),
Reason = Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionReason.SavingData
})

var suspend = new Action(async () =>
{
try
{
Expand All @@ -717,8 +716,27 @@ private async void HandleSuspending(object sender, SuspendingEventArgs e)
DebugWrite($"Calling. OnSuspendingAsync {(OriginalActivatedArgs as LaunchActivatedEventArgs)?.PrelaunchActivated ?? false}", caller: nameof(OnSuspendingAsync));
await OnSuspendingAsync(sender, e, (OriginalActivatedArgs as LaunchActivatedEventArgs)?.PrelaunchActivated ?? false);
}
catch { /* do nothing */ }
catch (Exception ex)
{
DebugWrite($"While suspending {ex} {ex.Message}", caller: nameof(HandleSuspending));
}
finally { deferral.Complete(); }
});

if (AutoExtendExecutionSession)
{
using (var session = new Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionSession
{
Description = this.GetType().ToString(),
Reason = Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionReason.SavingData
})
{
suspend();
}
}
else
{
suspend();
}
}

Expand Down

0 comments on commit 0b82fed

Please sign in to comment.