18

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?

10
  • Seems like that you are not missing anything but I cannot find anywhere in documentation that says "Revoked event should be fired upon resuming the app". Maybe you need to put some extra code in OnResuming instead. Mar 22, 2016 at 22:12
  • @MehrzadChehraz No - the resuming event seems to be fired only after the suspending event is finished. This means that in release mode the app will be killed by the OS due to some time limits and won't rise revoked and resuming events at all.
    – Romasz
    Mar 22, 2016 at 22:14
  • Yes you are right. By the way, can you add a link or something to the documentation that says "Revoked event should be fired upon resuming the app"? Mar 22, 2016 at 22:18
  • @MehrzadChehraz I take it from here at MSDN - one of the two resons is app resumed, which makes sense - for example to pass cancellation signal, or fire different resuming method. As I've tested the above code, the order of debug output is correct - if you fire suspend and resume right away, wait few second, you will see that revoked event is fired before 'Executing finished'. Problem is that somehow you will see the output only after everything finishes.
    – Romasz
    Mar 22, 2016 at 22:26
  • @MehrzadChehraz In release mode when PLM is disabled, the app doesn't have so much time so it will be terminated. I can just suspect that this is because UI thread is busy by awaiting in extended execution session. But IMHO, this doesn't make sense to use ExtendedExecutionSession in this case at all - if there is a big chance that the app will crash once the user gets back to it - more over you cannot say at what moment it will crash - for example in the middle of saving the file. Therefore I looking for things that I've missed, maybe just my thinking is wrong.
    – Romasz
    Mar 22, 2016 at 22:33

2 Answers 2

1

The usage of the await and Task is causing that your continued Task remains on the main thread making you have to wait with the black screen. Remember that the await behavior is to schedule the execution into the Dispatcher, NOT to begin a new thread, NOR schedule its execution into the ThreadPool. As consequence, No more UI messages can be processed until the Delay() ends.

Just perform your time-consuming operation on a new thread, but ensure to keep the session open until it ends.

Take a look of this https://msdn.microsoft.com/en-us/magazine/jj991977.aspx to get a good insight about how execution is sheduled

1
  • Any ideas how to perform time-consuming operation on a new thread and ensure to keep the session open until it ends, without using await on UI thread?
    – Romasz
    Mar 30, 2016 at 4:28
1

There is no UI problem or anything. Your code works. Your expectation is wrong.

Using the ExtendedExecutionSession you're telling your app you need time to save and it shall not be revoked until you are finished. In your case that takes about 9 seconds.

Try suspending the app, wait for 10 seconds an then revoke it. It will happen immediately. Then try suspending the app and revoke it before the session is finished. Now the ExtendedExecutionSession will tell your OS that your app cannot be revoked yet and it has to wait until the saving process is finished. That's what you want.

See Microsoft doc on extended execution:

Requesting a ExtendedExecutionReason.SavingData extended execution session while the app is in the Suspending state creates a potential issue that you should be aware of. If an extended execution session is requested while in the Suspending state, and the user requests the app be launched again, it may appear to take a long time to launch. This is because the extended execution session time period must complete before the old instance of the app can be closed and a new instance of the app can be launched. Launch performance time is sacrificed in order to guarantee that user state is not lost.

What's mentioned in the section about "Revoke" is interesting for you, too:

When the Revoked event is fired for an ExtendedExecutionReason.SavingData extended execution session, the app has one second to complete the operation it was performing and finish Suspending.

One second is not enough to finish your 9 seconds waiting.

To eliminate the possibility of delayed display of your debug output, you can test it with adding the current time to the output. The OS probably has a problem with the session not closing properly because the 9 seconds don't finish.

Also note the remark on EnterBackground:

Previously your suspending callback was the best place to save state after a user finished a session with your app. However, now an application may continue running in the background and then move back to the foreground due to trigger activity without ever reaching the suspended state. The best place to save data after a user session is in your entered background event handler.

You will probably want to do your code in case the Exiting event is fired.

For OnSuspending try doing the waiting with a for loop that breaks (cancelling the saving process) as soon as the revoke happens, only waiting for half a second at a time.

UPDATE:

...Or use a Background Task, as suspending seems to be the only reliable warning before termination:

//
// Declare that your background task's Run method makes asynchronous calls by
// using the async keyword.
//
public async void Run(IBackgroundTaskInstance taskInstance)
{
    //
    // Create the deferral by requesting it from the task instance.
    //
    BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

    //
    // Call asynchronous method(s) using the await keyword.
    //
    var result = await ExampleMethodAsync();

    //
    // Once the asynchronous method(s) are done, close the deferral.
    //
    deferral.Complete();
}

UPDATE2:

For the "proper" way how it should be done, see the official example:

private async void OnSuspending(object sender, SuspendingEventArgs args)
{
    suspendDeferral = args.SuspendingOperation.GetDeferral();

    rootPage.NotifyUser("", NotifyType.StatusMessage);

    using (var session = new ExtendedExecutionSession())
    {
        session.Reason = ExtendedExecutionReason.SavingData;
        session.Description = "Pretending to save data to slow storage.";
        session.Revoked += ExtendedExecutionSessionRevoked;

        ExtendedExecutionResult result = await session.RequestExtensionAsync();
        switch (result)
        {
            case ExtendedExecutionResult.Allowed:
                // We can perform a longer save operation (e.g., upload to the cloud).
                try
                {
                    MainPage.DisplayToast("Performing a long save operation.");
                    cancellationTokenSource = new CancellationTokenSource();
                    await Task.Delay(TimeSpan.FromSeconds(10), cancellationTokenSource.Token);
                    MainPage.DisplayToast("Still saving.");
                    await Task.Delay(TimeSpan.FromSeconds(10), cancellationTokenSource.Token);
                    MainPage.DisplayToast("Long save complete.");
                }
                catch (TaskCanceledException) { }
                break;
            default:
            case ExtendedExecutionResult.Denied:
                // We must perform a fast save operation.
                MainPage.DisplayToast("Performing a fast save operation.");
                await Task.Delay(TimeSpan.FromSeconds(1));
                MainPage.DisplayToast("Fast save complete.");
                break;
        }

        session.Revoked -= ExtendedExecutionSessionRevoked;
    }

    suspendDeferral?.Complete();
    suspendDeferral = null;
}

private async void ExtendedExecutionSessionRevoked(object sender, ExtendedExecutionRevokedEventArgs args)
{
    //If session is revoked, make the OnSuspending event handler stop or the application will be terminated
    if (cancellationTokenSource != null){ cancellationTokenSource.Cancel(); }

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        switch (args.Reason)
        {
            case ExtendedExecutionRevokedReason.Resumed:
                // A resumed app has returned to the foreground
                rootPage.NotifyUser("Extended execution revoked due to returning to foreground.", NotifyType.StatusMessage);
                break;

            case ExtendedExecutionRevokedReason.SystemPolicy:
                //An app can be in the foreground or background when a revocation due to system policy occurs
                MainPage.DisplayToast("Extended execution revoked due to system policy.");
                rootPage.NotifyUser("Extended execution revoked due to system policy.", NotifyType.StatusMessage);
                break;
        }

        suspendDeferral?.Complete();
        suspendDeferral = null;
    });
}
10
  • I still don't see the usage of Revoked for me - for now I can use Resuming, LeavingBackground, Launching method. I would expect the revoked event to run on separate thread, so I can pass cancellation tokens to my main task or do something. For now it runs on the same thread and is blocked untill extended execution finishes.
    – Romasz
    Aug 10, 2017 at 20:38
  • That means that you have to use the extended execution on a different thread and not block the Suspending method.As Suspending expects you to finish in less than a few seconds and you need more time, I think this might help. I'd start a backround task from Suspending and use extended execution there. That way, it will always (try) finish saving no matter if you revoke your app or not during the saving process. If the data became corrupted when the app was running, you should abort the saving process in Revoked. Aug 17, 2017 at 7:18
  • Background Task is a different manner and it has nothing to Extended execution, as I think. Take a look that already my sample runs on a new thread, what shouldn't block the normal UI from work.
    – Romasz
    Aug 17, 2017 at 7:54
  • You're right that it should not block your UI thread. But it's blocking the thread where the suspending and revoked events are triggered. Thus your problem that revoked is waiting for suspending to finish. So I suggest you run a background task and do not block suspending so that revoked event can fire as expected. I guess you're right that you don't need extended execution in the background thread... Ah yeah of course, you're right, extended execution is not what you want to happen as you expect your app to respond immediately no matter if the saving process is still running or not. Aug 17, 2017 at 8:12
  • That's why I think it's 'not working properly' - you are running something on background thread, expect to have responsive UI, but it's not, hence Suspending/Resuming is waiting for you background thread to finish, whereas it should just run normally on UI thread (as I think).
    – Romasz
    Aug 17, 2017 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.