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.
Related
I'm new to the UWP, and I'm facing right now an issue where I want to continue an operation that the app was doing before getting into suspended or terminated state.
I've read about Extended execution and background task, but as far as I have understood for extended execution you have only 30 seconds before it gets terminated or it could be revoked before getting into it.
for Background task I should make another project for it ( Windows Runtime Component) and I have to add an entry in the Declarations in the appxmanifest. It sounds that Background task is the only possible way to achieve it, but how to move an operation (Action, Func, or task or whatever) to background task if the app get into those states and what should happen after resume?!
Any ideas from experienced people?
Windows 10 universal Windows platform (UWP) app lifecycle
Before Windows 8, apps had a simple lifecycle. Win32 and .NET apps are either running or not running. Now, there are three app model in UWP app Running in foreground,Running in background and suspended state. You could know more detail through this official document.
Extended execution
There are cases where an app may need to keep running, rather than be suspended, while it is minimized. If an app needs to keep running, either the OS can keep it running, or it can request to keep running.
For this scenario, you need use ExtendedExecution to realize. ExtendedExecution support to start a long running operation in order to defer the Suspending state. And there are some document and code sample introducing this feature.
Background Task
For Background Task, it provide functionality when your app is suspended or not running. You can also use background tasks for real-time communication apps like VOIP, mail, and IM. However, it will trigger under specific conditions. For more please refer this.
You have mentioned out-process Background Task in your case and another Background Task(in process) could also be used in UWP app. It is simpler to implement than out-of-process background tasks.
I have been testing a C# Companion Device Framework application, which unlocks my laptop fine for the most part. However, it doesn't seem to work after I leave my laptop locked for a while.
I used the code from the CDF GitHub sample to fire a toast notification when the background task for my UWP companion app is triggered.
This shows me that there is never an issue when I attempt an unlock shortly after locking the machine. When I lock my machine, I immediately see the toast notification indicating that the background task was triggered. However, if I lock my laptop and leave it for a few minutes, it doesn't appear that the background task gets triggered again, even though I wake up the screen and press buttons.
I want my CDF app to always be able to unlock my machine. What did I do wrong? Hopefully I don't need it, but is there a workaround like registering a second trigger for the background task to a custom service?
UPDATE: It appears this occurs only if the computer does not go to sleep, which may occur in the case that someone has either set a long time before sleep or has sleep off completely (as I did previously). If the laptop does go to sleep, and has to be woken up with a trackpad click, then the background task seems to fire.
The problem is, Windows UWP stops to fire the event WaitForUserConfirmation after awhile.
At present, we have 2 possible solutions:
user hits the keyboard and the background task catches the event CollectingCredential and invokes the companion device authentication
once the background task is running, it loops until the event CredentialAuthenticated, and it runs the companion device authentication periodically.
I created app for universal windows platform(for Windows 10 desktop). I use DispatcherTimer in app. Timer run async method. When app in foreground it is work.
But app is background(I minimize a window) async method not work. How can I solve this problem?
When your app is not running in the foreground and tasks need to be executed, it is well known that we need to implement the background tasks for the app.
But, background tasks in UWP are lightweight. Due to memory constraints, battery life issue, I'm not sure what you need can be done in background task of UWP.
That doesn't mean you can't use DispatcherTimer in the background task, but background tasks are limited to 30 seconds of wall-clock usage, and it can be terminated by system for example when it throw the out-of-memory exception. So, if you want to execute your task every one minute, then it will not work.
The TimerTrigger which is mentioned by #ibebbs has a minimal time intervals which is 15 minutes, so I'm also not sure this can be used in your scenario.
Problem is what you need to do in the background task and how often, you can leave a comment to tell that, so can we continue to discuss on this issue.
You should Create and register a background task that runs in a separate process and use a TimerTrigger to invoke it at the desired interval.
I've posted previously about synchronisation issues with my background scheduled task having to access a SQLite DB and IsolatedStorge that the foreground app uses.
To simplify the process I thought about just preventing my background task from running altogther, it isn't imperative that it runs, especially when the foreground app is active.
Is there a way to do this?
I thought about using IsolatedStorage to set a flag when the app launches then remove it when it exits, then have the background task check the setting, protected by mutex.
I think the idea is fine in principle, but I guess there will be times when the flag isn't unset, for example if the battery dies... Which means potentially, after turning the phone on, if the user never uses the app and exits properly, the background task will never run. This might not be too much of an issue.
Is using some kind of flag like this the only way to achieve such functionality?
Thanks
There is an inter process communication mechanism to achieve this functionality.
As both the foreground app and scheduletask run as different processes, IPC could be used for it.
Please refer to Named Events.
In your OnInvoke Method subscribe for a named event which will be fired by your foreground app as soon as it is launched/resumed.
As soon as you get a signal via this event in background agent, just call NotifyComplete and you are done.
I have developed two applications and they are both using a background agent. When I turned on Airplane Mode last night, and woke up tomorrow I saw (Settings > Background Tasks) that 1 Background Task was running fine, and the other was not.
The one that was disabled gets data from a Weather API, and the other just generates random tiles (no internet connection).
When I opened the app, and went back to the Background Tasks it enabled itself.
What is going on here? I know that a background task will disable itself when the app is not used for 14(?) days, but this is not the case. And of course I know that the background task won't run when the phone is in Airplane Mode.
So, how can I enable my app to turn itself on again when the Airplane Mode is turned off? Just like my other (offline) app does.
Kind regards,
Niels
It probably means that your background task doesn't handle graciously no-connectivity errors. When your background agent fails two times in a row, it's automatically disabled by the OS.
Just put your network call in a try/catch block to solve the issue.