Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to to determine when AppService is closed due to UWP app suspending or full-trust process closing?

Consider the following example: https://github.com/StefanWickDev/UWP-FullTrust/tree/master/UWP_FullTrust_3

When the UWP app creates the AppService, it will observe the related task cancellation in order to detect the closing of the AppService. The AppService gets closed when:

  1. The full-trust process drops the AppServiceConnection.
  2. The UWP app is suspended.

The logic I want to implement is to restart the full-trust process if the connection was dropped because of the full-trust process going away (e.g. a crash), but avoid restarting if the UWP app is going to suspend. I have not found a reliable way to do this.

In App.OnTaskCanceled there's nothing in the parameters that would indicate what caused the closure (SystemPolicy for both cases). App.IsSuspending is false at that point. The App.Suspending event always happens after the App.OnTaskCanceled has been triggered.

I could start a timer and delay the restart trusting that App.Suspending gets triggered in a time-window, but I would prefer to use a less complicated way.

BTW, it is not clear to me if the UWP app can be suspended if the AppServiceConnection is open. At least VS allows me to trigger the suspend/resume using the lifecycle controls and the UWP app's Suspending/Resuming get triggered.

Suspend/resume behavior seems to be undocumented for Desktop Bridge UWP apps, so I'm essentially trying to reverse engineer how this works...

like image 988
DesktopMonkey Avatar asked Dec 11 '19 01:12

DesktopMonkey


People also ask

How do you tell if an app is UWP?

If the file location is the WindowsApps folder in Program Files, or Windows 10 refuses to open the folder, then it is a UWP App, because Win32 apps are stored in their own folder in Program Files (x86) and 64bit applications are stored in their own folder in Program Files . . . Power to the Developer!

How do I suspend my UWP app?

When you're debugging an app, you can send it a suspend event using Visual Studio. Make sure the Debug Location toolbar is being shown, then click the Suspend icon.

What is UWP process?

Universal Windows Platform (UWP) is a computing platform created by Microsoft and first introduced in Windows 10. The purpose of this platform is to help develop universal apps that run on Windows 10, Windows 10 Mobile, Windows 11, Xbox One, Xbox Series X/S and HoloLens without the need to be rewritten for each.

What is app close confirmation in UWP?

App close confirmation in UWP. It is a common request from app developers to be able to display a confirmation dialog when their app is being closed. Although this scenario is becoming less and less relevant in days of mobile apps and cloud-based data, it is still useful in specific cases.

What happens when a UWP app is suspended?

A UWP app is suspended shortly after the user minimizes it or switches to another app. This means that the app's threads are stopped and the app is left in memory unless the operating system needs to reclaim resources. When the user switches back to the app, it can be quickly restored to a running state.

Why would I want to call close on an app service?

But you might choose to call Close if you want to stop the app service regardless of outstanding references to the app service connection. After calling Close in a situation like that, we recommend that you consider the object to be invalid, and not to use it further.

What happens when you close an app in Windows 10?

The terminated app will still appear in the task bar. When the user click on it, the app must restore the state that it was in before it was terminated because the user will not be aware that the system closed the app.


1 Answers

You can use the EnteredBackground/LeavingBackground events to distinguish the two scenarios.

Here is a full sample showing this approach:
https://github.com/StefanWickDev/UWP-FullTrust/tree/master/UWP_FullTrust_3

Related blog post:
https://stefanwick.com/2018/04/16/uwp-with-desktop-extension-part-3/

like image 71
Stefan Wick MSFT Avatar answered Oct 22 '22 10:10

Stefan Wick MSFT