2

My question is similar to How to make UWP app to suspend and resume state, but I need an application that I can give to my QA team so that they can more easily invoke Suspend and Resume in my app.

Since Visual Studio has a "LifeCycle Events" toolbar that lets you suspend and resume your App, I figure that there must be an app that ships with Visual Studio that does this. However, perusing through the Visual Studio files, I was not able to find such an executable.

Does anyone know of a stand-alone application (installed with Visual Studio or not) that can suspend or resume a windows store app?

If not, does anyone have sample code that can suspend or resume an arbitrary UWP app? I understand that there are some C++ libraries for building a debugger, but I'm not a C++ programmer. If there's a C# way to do this, please post some code. If it must be a C++ application, please post a complete example that is easy to build.

Greg Thatcher
  • 1,303
  • 20
  • 29
  • Can you enter tablet mode and switch the application? – lindexi Dec 15 '18 at 01:25
  • I have tried that, as described in the other stack overflow article linked to above, but that does not seem to cause a suspend on devices/desktops with plenty of memory. I believe the newer Foreground/Background lifecycle model makes this work differently. – Greg Thatcher Dec 15 '18 at 02:10

2 Answers2

3

UWP provides dedicated APIs for suspending and resuming apps: StartSuspendAsync StartResumeAsync

Here is for example how you can suspend the FeedbackHub app:

var diag = await AppDiagnosticInfo.RequestInfoForPackageAsync("Microsoft.WindowsFeedbackHub_8wekyb3d8bbwe");
if (diag.Count > 0)
{
    var resourceGroups = diag[0].GetResourceGroups();
    if (resourceGroups.Count > 0)
    {
        await resourceGroups[0].StartSuspendAsync();
    }
}

Note that you will need to declare the 'appDiagnostics' capability to call these APIs:

<Package
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp rescap">
  ...

  <Capabilities>
    <rescap:Capability Name="appDiagnostics" />
  </Capabilities>
</Package>
Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
0

One possibility would be for the tester to simply minimize and maximize the application. This would trigger a suspension and resumption.

To check whether the application has really been suspended and resumed, you can use logging, e.g. MetroLog or any other logging solution.

For a quick test one could do this:

MetroLog

In the Package Manager Console enter:

Install-Package MetroLog 

Code

In the App constructor add something like:

LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new StreamingFileTarget());
log = LogManagerFactory.DefaultLogManager.GetLogger<App>();

this.Suspending += OnSuspending;
this.Resuming += OnResuming;

Then there are these two methods:

private void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    log.Trace("OnSuspending called");
    deferral.Complete();
}

private void OnResuming(object sender, object e)
{
    log.Trace("OnResuming called");
}

Test

  • deploy App
  • quit VS
  • call the application from Windows menu
  • minimize and maximize the appliction

In the folder ApplicationData.Current.LocalFolder you will find a new folder MetroLogs with a file named similar to Log - 20181216.log.

Open it in a text editor:

log output

As you can see the application was suspended and resumed.

Is that what you're looking for?

Stephan Schlecht
  • 26,556
  • 1
  • 33
  • 47