I'm trying to stop my webcam preview when my app suspends (because it doesn't like resuming).
I'm following the 'Getting Started' guide on MSDN here
I've added await mediaCaptureMgr.StopPreviewAsync(); to the SaveState async method (that's used in example apps).
This works perfectly using Visual Studio's 'Suspend' option (in the Debug Location toolbar) but SaveState doesn't fire when the app is suspended legitimately by switching apps in Windows itself.
I've tested this with the default grid project and the same thing happens (fires when 'Suspending' with VS but not by switching away from it)
Am I supposed to be doing this a different way?
Cheers,
Nick
App suspension does not occur immediately after an app is no longer visible. There is a delay of about 10 seconds before an app is suspended. This (and more detailed) information can be found in this Application lifecycle article.
Also, Task Manager provides a convenient way to view if an app has been suspended. By default, the suspended status is not visible, but can be enabled as shown below.
Related
Controlling suspending, resuming and starting Windows Phone Apps with Windows RT, there are 3 events:
OnLaunching -> OnLaunched respectively
OnSuspending
OnResuming
Normally if an app launches, OnLaunching is fired.
When the app suspends e.g. the screen saver comes up, OnSuspending is fired.
If I go back to the app by opening task list, OnResuming is fired.
Now I have the following problem.
If I switch to another app or the screen saver appears, OnSuspending is fired - this is OK.
But if I go back to my app via task list after about 15-20 minutes, the OnLaunching event is fired instead of OnResuming, although my app appears still in the task list.
It's like the app is newly started !
This happens since I redesigned the app in Windows 8.1 RT.
In the earlier version of my app, which was running in Silverlight, this worked fine. Whenever I went back to my app via task list, OnResuming was fired.
Doing some tests, I found out the following:
Launching an app, the previous execution state can be read from the ActivateEventArgs. So I read them out to see, what application status was before launching.
After starting the app the first time, its state is 'notRunning'.
If I close the app and reopen it, the state is 'closedByUser'.
If the app has been suspended and I revoke it after 15-20 minutes, the state is 'terminated'.
However, the state 'terminated' means, the windows has been closed the app.
Why does windows close my app already after 15-20 minutes and why the app is still in the task list if it has been closed ?
Do you have an idea, what the problem is ?
I was told by Microsoft:
The OS can terminate an app in order to free up resources. But such an up can still appear in the task bar.
If the suspended app was terminated, there is no Resuming event and instead OnLaunched() is called with an ApplicationExecutionState of Terminated. So, the routines to restore the app state must be called within OnLaunched().
Sometimes it's required to distinguish if the app has been relaunched by tasklist or by tile. For this, use the TileId property in the OnLaunched(). TileId is "App" if the app has been started by tile and it is empty if the app has been relaunched by tasklist. TileId could also have another value if you generate eg. a second tile for the app giving them another id.
However, this does not work in Windows 8.1 as the value of TileId is always "App" independent of starting it by tasklist or standard tile.
In my Universal app, (Windows 8.1 Windows Phone8.1), I want to know the reason why my app is suspended like due to a launch of a launcher(Mail, File picker etc) or user pressed the Windows button. Is there a way to determine this?
Unfortunately you can't know what caused the app's suspension. Unlike the LaunchActivatedEventArgs in the OnLaunched method for example, that contain the Kind property the SuspendingEventArgs in the OnSuspending method don't provide any such information.
However, there are only so many reasons that an app can be suspended. Like you said (in windows phone only) it can be suspended due to a launch of a launcher or a protocol and by the windows button.
The windows button is the only suspension way of which you have no control. For all the others you can trick the system and for example set a static global variable that you update when you launch an operation that would suspend your app and check it in OnSuspending
For security reasons, I need to log out the users when they exit the app and show login screen when they return back.
In Windows Phone 8 and Windows Phone 8.1 Silverlight there are Application_Deactivated and Application_Closing methods on the App class (or methods OnClose, OnDeactivate to override in Caliburn.Micro).
The only interesting events seems to be Suspend and Resume, but they do not called when I exit the app using the Start button and get back using the Back button or launching the app from the list.
What are the alternatives for Windows Phone 8.1 XAML?
(Setting ActivationPolicy="Replace" would solve half of the problem but I guess this is not possible, when WMAppManifest.xml is not event a part of a Windows Phone 8.1 XAML project).
The Suspending event will be called just after you navigate away from the app, but not in debug mode. I've build a simple app modyfing LocalSettings upon Suspending event and then acquiring information when Resuming.
You are probably aware, but for the sake of completeness of the answer - some remarks:
before Suspending event, the OnNavigatedFrom event is being called, but when you Resume, the OnNavigatedTo is not called - reference:
Note On Windows Phone, OnNavigatedFrom() is called when the app is suspended. OnNavigatedTo() is not called when the app is resumed.
to test Suspending/Resuming with debugger, use Lifecycle events in Debug location tab - more info
reference to Application lifecycle in Windows Runtime apps
I am developing an app. Which uses live tiles with background task (as WinRT Component) as I want to updates tile with time trigger. I have noticed that if I set permission of lock screen to ON and let the background task run then app always remains in suspended mode after I close the app. So how can I kill the app and let the background task run on it's own ? I am following this sample.
You can forcefully close an app in a number of ways:
Alt+F4
Swipe the app from the top of the screen to the bottom
Use the task manager
You cannot programmatically kill your app (well you can if you introduce a NullPointerException (DON'T DO THAT) or if system memory requirement kicks in (system needs memory for other tasks so OS will kill your application.).
please read following article on Windows 8 application lifecycle.
Application Lifecycle
I have a graphical control that is multithreaded.
Until now it worked fine, but I just noticed that whenever I'm on my application showing this control the following happens: if I lock and unlock my workstation, it freezes, like if it were in some kind of infinite loop.
Even stranger, this bug occurs only when I'm not launching the app from Visual Studio, and not attached to it.
Does anyone have a clue on what's happening?
For instance, if I attach Visual Studio to the already freezed app, can I see which lines of code my threads are executing?
Any help will be appreciated!
We recently had (for about a year and a half ;) this same problem. It also triggers sometimes when IE flushes caches, when you change colors of you theme. etc.
The problem was that we had a splash screen that had its window created on one thread and then it was shown (ShowDialog()) within other thread. Once we moved the window creation to the same thread that actually shows it, it resumed. There was also some changes with .Dispose():ng the splash window, and they could also have had an effect.
Microsoft has an article about this and they basically suggest to run their Spy++ program and look at your program when it's hung. There is a "Windows" -window, search for your application and look for any windows that should not be there. They possibly have a windows message pump active/attached but it is not pumping. The "change" message does not get handled and all .NET windows stall -> hang.
just attach VS to the frozen app and hit Pause button, VS will show executing code.