0
votes

Using Windows phone 8.1 silverlight Background Task is a WinRT Task.

The problem I'm facing is I'm running a background task and I want the background task to exit when the foreground App is closed or terminated. I know when closing the foreground App I can use the Application_closing method to write to isolated storage to communicate with the background task. But the real question is how do I handle the event when the foreground App goes from suspension state to terminated state. Or even if is it possible for the background task to query the status of the foreground App to the OS. Thanks you.

1
What type of background task are you writing, and when exactly do you need it to shutdown (suspension, losing foreground status, termination)?Peter Torr - MSFT
@Peter I'm trying to schedule a toast notification at a certain time in the background but I don't want the toast notification when the foreground App is closed or terminated. Hence I would like to un-schedule it if the App is terminated.AbsoluteSith
In previous releases, Windows Phone would suppress toasts if the app was in the foreground... not sure if that still happens or not. Anyway, one way you could figure this out is with named events - see this article and associated codePeter Torr - MSFT
What is the purpose in differentiating between suspended and terminated state of your app? In both cases your foreground code is not running so what's the benefit in knowing this?Jimmy Alexander
@Jimmy Alexander : Well for a programmer we know the app is suspended but as a end user will always think the app is still running. In my case its like a simple countdown timer if the app is suspended I still want to send him a notification when the timer hits 0. But don't want to do this if the app is terminated.AbsoluteSith

1 Answers

1
votes

Unfortunately there is no way to get information about your app going from the suspended into the terminated state. That's why most articles on MSDN explicitely state that you have to save any session related data before an app is suspended.

You should always save user information and app data in the suspending event because Windows doesn’t notify apps before it terminates them. This is important because termination can occur under a variety of circumstances, such as when Windows needs to free memory or the device loses (battery) power.

https://msdn.microsoft.com/en-us/magazine/jj660301.aspx

What you could do is to implement some kind of ping mechanism where your foregound app constantly writes timestamps to the isolated storage. If these pings exceed a predefined timestamp you can assume your app was terminated and exit the background task.