Suspending Event Not Raising Using Winrt

Suspending event not raising using WinRT

Suspending event won't fire while you are debugging (but while running your App normal, it will fire just after you navigate away from the App) as it is said also at this blog:

...you will wait forever for these to trigger, even though your app switches back and forth to the screen! The reason is simple: while an app is being debugged, Windows will not suspend it.

Note that this may lead to some weird app behavior, when there is something wrong in Suspending event - for example if you pass some complex class in Frame.Navigate method and you use SuspensionManager. While debugging your app will work just fine (no suspension), but will crash without debug mode.

To test how your App behaves, you will have to invoke the Suspending manuallt, open (or set visible) Debug location toolbar in Visual Studio, there you will find a dropdown Lifecyce events, choose there Suspend, and then to return the App - Resume.

Csharp Sample Image 6

WinRT/UWP is suspending rising on power off?


Is suspending action rising on long power off button holding?

System will shutdown forcibly with long press power off button. And the system could not make sure Current user session is finished. So the suspending event handler could not be invoked correctly.

From Windows 10 universal Windows platform (UWP) app lifecycle:

Current user session is based on Windows logon. As long as the current user hasn't logged off, shut down, or restarted Windows, the current user session persists across events such as lock screen authentication, switch-user, and so on.

So, before shut down, the app still in the Current user session. And Suspended will be invoked on power off(shut down).

Note, you can not test it in debug model within Visual Studio. Because when you shut down the system, Visual Studio will exit degbug model at first. The Suspended event will not be invoked as expect. You could verify with following code.

private void OnSuspending(object sender, SuspendingEventArgs e)
{
var stringBulider = new StringBuilder();
var deferral = e.SuspendingOperation.GetDeferral();

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Object value = localSettings.Values["exampleSetting"];
stringBulider.Append(value.ToString() + "/Next");
localSettings.Values["exampleSetting"] = stringBulider.ToString();

deferral.Complete();
}

Each time you shut down, the stringBulider will be append one at a time.

uwp suspending event not working

You can't show MessageDialog in Suspending event - this event is raised as user already has left your app. You have very limited time to do something (like saving state of the app), if time is run out, the OS will terminate the process.

You can't prevent user from leaving your app - this is by design.

If you want to debug your event - take a look at this answer.

OnSuspending event not triggered with UWP Windows 10

From the official App lifecycle documentation:

A note about debugging using Visual Studio: Visual Studio prevents Windows from suspending an app that is attached to the debugger. This is to allow the user to view the Visual Studio debug UI while the app is running. 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.

That means that the OnSuspending event won't get fired while you are attached to the Visual Studio debugger. If you want to debug it, manually send the event by selecting the respective Lifecycle Event.

Sample Image

Windows phone 8.1 (WinRT) and Windows phone 10 (UWP) app suspending procedure

Here is a very good article about RT app life cycle: What You Need To Know About Suspend and Resume in WinRT.

If you don’t call Complete method before the SuspendingOperation.Deadline expires, a timeout occurs and Windows guesses that something bad happened to your app… and, instead of being suspended, it will be terminated.

So, you should finish your job and call Complete before Dedline.

The break point in c++/WinRT Component code is not triggered

this is the answer from @YanGu - MSFT

If you want to trigger a break point in your C++/WinRT Component, the
component project should be in the same solution as the UWP app. Then
set the debugger type as Mixed(Managed and Native)




Related Topics



Leave a reply



Submit