When my windows store app goes to the background it still keeps running.
According to the documentation of the App.Suspend event the app should suspend after a few seconds when in background.
I tried running the app on a simulator and on a real device, both with a debugger attached.
Is the debugger preventing the app from suspension? Or am I mistaken that the app suspends after a few seconds when it is in the background?
Yes, the debugger prevents suspension. In Visual Studio, however, you can force suspend, resume, and suspend+terminate. There's a dropdown on the toolbar for this purpose when you're running the app, which lets you trigger background tasks for debugging as well.
Related
My WP 8.1 store app behaves very strangely. App Resuming event fires as expected when I quickly navigate away and then come back. But If I keep the app in background for some time and when comes back, the app fires the Constructor and OnNavigatedTo events instead of the Resuming event and has the black "Resuming..." screen for a couple of seconds(about 4 seconds).This is an app with a BackgroundAudio task. Even the sample BackgroundAudio app from MS behaves like this. Anyone knows what's wrong here?
I'm in the process of building a WP 8.1 app which uses a background audio task as well. Everything you explained happens to me also.
If you see "Resuming..." for a few seconds, then it probably means your app was terminated by the OS after being suspended first. In this case, the Resuming event won't fire because your app was completely killed and must start again. "Resuming" usually means when a suspended (not terminated) app resumes execution.
When your app is terminated and then "resumed" from the app switcher, your Application.OnLaunched() method override will be invoked with e.PreviousExecutionState == ApplicationExecutionState.Terminated. In this method, you should check if the previous execution state was Terminated, and if so, restore the app to the state it was in prior to suspension. This gives the illusion to the user that the app was never terminated and they can resume what they were doing at the time.
If you create a new Pivot App Windows Phone 8.1 project (for example), you'll see that the application lifecycle events are taken care of correctly in App.xaml.cs.
App Resuming event fires as expected when I quickly navigate away and then come back.
This is correct behavior. It takes a few seconds once the app has been backgrounded before it is suspended by the OS, so if you resume the app before the OS has suspended it, then it will simply resume from memory.
I'm not sure why background audio apps are more susceptible to termination. I even find that the Xbox Music app behaves similarly. Hopefully in the next version of Windows Phone, this issue will be addressed.
FYI here's a diagram of the application lifecycle from MSDN (I recommend you read this page for more information about the application lifecycle):
"Resuming" only occurs from the suspended to running states.
Whenever the app resumes from the background. there are two states it can be in:
Suspended: it resumes successfully, navigating directly to OnNavigatedTo; the constructor will not be called
Terminated: the app will not start from the previous state of the page you left, but instead, it will load that page anew; at this point of time you need to have saved the state while the app was suspending, so as to restore it now
Issue
I want to use a background task to show toast notification in windows phone 8.1. I followed this article to add the background task Microsoft article. The problem is that the background task never runs. Its runs only when I call it from lifecycle events when In debug mode.
this is very tricky. but i assume there might be a lot of other background tasks running at the sametime which are not allowing your task to run.
try restarting the phone and then launch this app first. that should solve it.
How do we force an app to Suspend (NOT shutdown, just suspend) and then force-resume it ourselves when we are NOT using visual studio/debugging?
When running under Visual Studio 2012, you can enable the Debug Location on the debugger toolbar. This will allow you to select one of the three operations, "suspend", "resume", and "suspend and shutdown".
[edited]
Sorry, just noticed that you are asking for non VS solution. You can suspend your app by dragging the app down. Move your cursor to the top of the page until you see a hand. Click and hold it and pull down to suspend your app. It takes a few seconds before the app actually enter suspend mode. If you want to do things sooner, you can use visibility change to toggle certain action first, like saving critical stuff.
How can you tell when a windows 8 Metro app gets put in the background? The suspended state doesn't activate. I have a break point on. It only hits if I close the app.
I am using a webcam and since no apps can run in the background I need to save my work when it's put in the background.
The windows phone it was application deactivated.
any help would be nice.
Apps do not normally get suspended when in the debugger. However, you can force a suspend when debugging by:
Enabling the Debug Location toolbar (red arrow in image below).
Then press the Suspend button (blue arrow).
The suspending event should fire when the application is no longer active, namely, when another application is brought to the front. Presuming you're using C#/XAML, the app.xaml.cs file already has the Suspending event wired up. In HTML5/JavaScript it's checkpoint and you'll see it in default.js.
I have a WP7 app that sometimes has to make a long (5-25 sec) processing.
With Mango, when the app is set to Dormant state while it was processing (ex: the user presses the Windows button or locks the screen), when the user comes back, the app crashes.
I tried on the emulator and on my device, same behavior.
If I reproduce it in debug mode on Visual Studio, it doesn't crash so it's hard to find what is really happening.
To reproduce it, start a new Windows Phone project, add a button on MainPage.xaml & add an event handler on the click event that executes an infinite loop:
while (true)
{
System.Threading.Thread.Sleep(100);
}
My question is: What is really happening? Why does it crash? Isn't fast app switching supposed to just pause the app process and resume it?
EDIT:
Another thing I noticed is that when running the heavy process, the deactivated & activated events do not seem to be raised when I get out/in the app.
Found the answer on the official Windows Phone forum (link). Here it is:
Why it crashes:
From the time the app is deactivated, it has exactly ten seconds to
finish up what it is doing. If the code takes more than ten seconds,
the OS will terminate the app.
Why the deactivated & activated events are not raised when I get out/in the app:
If the process is blocking the UI thread this also blocks the message
dispatch loop so I suspect that this is the reason why your app never
gets a chance to detect and handle the events.
Your problem is simple. WP7 cant really put your threads in a dormant state when you are under heavy processing in this fast time and will save a corrupt state. This state will crash when its reactivated.
It works in the debugger because the debugger makes everything slower and then WP7 has more time.
From Documentation:
When the user navigates forward, away from an application, after the Deactivated event is raised, the operating system will attempt to put the application into a dormant state. In this state, all of the application’s threads are stopped and no processing takes place, but the application remains intact in memory. If the application is reactivated from this state, the application does not need to re-create any state, because it has been preserved.
Source: http://msdn.microsoft.com/en-us/library/ff817008(v=vs.92).aspx