I'm not able to get ExtendedExecution
to work properly. The problem is that the Revoked
event is not being fired until the execution is finished. If we take a sample:
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
Debug.WriteLine("Suspending in app");
var deferral = e.SuspendingOperation.GetDeferral();
using (var session = new ExtendedExecutionSession())
{
session.Reason = ExtendedExecutionReason.SavingData;
session.Description = "Upload Data";
session.Revoked += (s, a) => { Debug.WriteLine($"Extended execution revoked because of {a.Reason}"); };
var result = await session.RequestExtensionAsync();
if (result == ExtendedExecutionResult.Denied) Debug.WriteLine("Extended execution failed");
else
{
Debug.WriteLine("Executing");
await Task.Run(() => { Task.Delay(9000).Wait(); Debug.WriteLine("Finished the task"); });
Debug.WriteLine("Executing finished");
}
Debug.WriteLine("Suspending after execution");
}
deferral.Complete();
}
The documentation states that Revoked
event should be fired upon resuming the app, but if you try the code with debugger attached, then you will see that debug output seems to look ok, but you have to wait 9000 ms for it to show up. This means that the code is suspended until the session finishes.
The biggest problem is that if you fire this without debugger attached, launch the app, suspend and then resume, you will see a black screen for few seconds and then OS will terminate your app.
Am I missing something? Has anybody got it working correctly?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…