'UWP Extended Foreground Session

So, I have made a few attempts to create an app in UWP, using C++/CX, and besides dreading the syntax, I had some fun. I ran into troubles when I attempted to prevent the app from being suspended. Initially I tried using this resource, but I have learnt that there is a timeout. Then I tried getting rid of said timeout (though I cannot remember, nor find the exact resource for that).

Finally, I have found the holy grail, but it still wouldn't work.

    auto ses = ref new ExtendedExecutionForegroundSession();
    ses->Reason = ExtendedExecutionForegroundReason::Unconstrained;
    ses->Description = "Michael";
    auto res = create_task(ses->RequestExtensionAsync()).get();
    if(res == ExtendedExecutionForegroundResult::Allowed)
    {
        ses->Revoked += ref new TypedEventHandler<Object^,ExtendedExecutionForegroundRevokedEventArgs^>(this, &App::OnRevoked);
        MessageDialog^ messageDlg = ref new MessageDialog("Allowed to exec");
        auto sh = create_task(messageDlg->ShowAsync());
        sh.then([this](IUICommand^ comm) {});
        Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
    }
    
    

In this code snippet I am creating an unconstrained session extension, and, if I get permission, I will also display a message box, so I know that it worked.

Both in debug, and at runtime, I have no issues, and the laptop I am running this app on is mostly on battery. But the suspension event is still triggered, and no revoke event is ever called.

I'll provide here both code snippets:

void App::OnRevoked(Object^ sender, ExtendedExecutionForegroundRevokedEventArgs^ e)
{
    MessageDialog^ messageDlg = ref new MessageDialog(e->Reason.ToString());
    auto sh = create_task(messageDlg->ShowAsync());
    sh.then([this](IUICommand^ comm) {});
}
void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
{
    MessageDialog^ messageDlg = ref new MessageDialog("Suspension event raised");
    auto sh = create_task(messageDlg->ShowAsync());
    sh.then([this](IUICommand^ comm) {});
}

My quest, as many others' before, is to find the workaround that will prevent this behaviour, and so my question resumes to this:

How do I prevent an UWP from being suspended, without any regard to the hardware it's running on ( it will run on a PC, be it desktop or laptop), and without a time or resource constraint?



Solution 1:[1]

I solved the issue, I ended up creating a new project from scratch in c#, and recreating the entire project (luckily I didn't have much trouble, since there wasn't too much to implement). In the end, it might have been something to do with either my xaml or some wrong API call.

Thank you Roy Li - MSFT for your time.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Konic2