Here's my scenario: I have a Windows Phone 8.1 App (MainApp), a Class Library and a Windows Runtime Component (BackgroundTask
) in the same solution.
When I load my App I register a TimeTrigger
BackgroundTask
and after that the OS handles this. There two events for the Task, the Run
method (inside the task itself) and the TaskCompleted
callback that I receive in my Class Library after the Task is completed. Both events are executed when the App is in the Foreground or Background.
When the App is shutdown, the BackgroundTask
is still there because it is the OS that handles it, it's always there. The thing is that I want to execute methods in my Class Library when the Run
method of the Task is executed, but since I have no instance of my App I can't do this.
So, how can I, from my BackgroundTask
when the App is shutdown, execute code from my Class Library or MainApp? Can I wake up/start the App to the Background or Foreground from my BackgroundTask
?
The OS doesn't allow you to open your app from your background task as that would violate the user's foreground experience as you would be changing it without their consent (which is why a toast like Rob Caplan mentioned would make sense instead). The TaskCompleted
handler you mention is meant in my opinion to allow you to allow your UI (which would be visible to the user) to react to the last completed background task instance of a registered task (ex: to show an updated album art that was downloaded in your bg task while your app was in foreground).
To run the code from the class library you mention in response to a completed task, just reference the class library (must be supported for reference by winRT library such as a portable library) or move the code to the background task winRT library and call that code from the IBackgroundTask.Run method before you exit.