I am developing an app in uwp where I dont want to exit the application on back button click(on mobile).Instead of exiting I want to run it in background(it also uses a timer which triggers every 5 second).
Could anybody please help me on this.
Thanks..
A few things to mention:
A UWP app does not exit on pressing back button. It generally goes into a suspended state. It starts running in background only if you have registered a background task.
A background task can be triggered using a time trigger. But the minimum time period between 2 triggers is 15 minutes. So, you might want to look into some other to handle this.
You might want to look at these if you want to understand more clearly
https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/
https://blogs.windows.com/buildingapps/2016/06/07/background-activity-with-the-single-process-model/
Related
I have an UWP app that is dependant on a background task. This task should thus be running all the time when the user is using the device. I have tried registering it with these triggers:
ApplicationTrigger _AppTrigger = new ApplicationTrigger();
builderInApp.SetTrigger(_AppTrigger);
builderSession.SetTrigger(new SystemTrigger(SystemTriggerType.SessionConnected, false));
builderPower.SetTrigger(new SystemTrigger(SystemTriggerType.PowerStateChange, false));
But it seems like sometimes the task does not start. It starts 90% of the time after a restart or shutdown, continues 100% of the time after energy-saving mode, works 100% of the time if the system is just idle, about 50% after a Hybernation and only rarely if I do Energy Saver and then hybernate without unlocking.
Is there something I can do to make this more reliable? I have already added <rescap:Capability Name="extendedBackgroundTaskTime"/> and enabled user management by User instead of windows.
Should I add some more triggers? Which ones? Is there another way to get better reliability? I don't mind using a .Net component if that would help, but my background task does have to be UWP.
Other question I have about this:
If I have triggered the background with trigger a, and trigger a fires again, does the process launch a second time, get cancelled and relaunched, or nothing happens?
Same question if I have triggered the background with trigger a, and trigger b fires, does the process launch a second time, get cancelled and relaunched, or nothing happens?
Edit: I have tried using StartupTask, but cannot get it to trigger after hybernation.
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 am learning to program in C# for .Net using VS2015 Community Ed.
Its my first question so if I am not in the correct format I apologize.
At present I am trying to click a button and have the program begin a countdown. This countdown item should be named (ie..Timer1) and its name appear in a ComboBox. That part is the easy part. The part where I am having an issue is figuring out how to have the timer continue if the program is terminated.
Is there a way to keep the countdown running after program termination until specifically terminated by the user? If so, What should I be searching for in order to learn this?
You could hide your form but the timer still continue, but there is no space for the timer to run if you close the application. It has to live in an application. I think what you're probably looking for is it living longer than your form, that is possible, just move the timer's scope up higher than the form, you'll have to play with the Program class, maybe make the form modeless instead of modal, or maybe not. You'll have to try a few ways. But the timer will have to run in your application, everything in your application dies when the application does.
The only way to have your timer continue to run after your process exits is to launch a separate process in which the timer will run. Of course, the original process will no longer have access to the timer, so you'll need to use some form of interprocess communication to allow the timer process to inform the original process of the timer state.
I'm working with background tasks.
If I press the home button once the app will call DidEnterBackground and I can run anything here - Ok
If I press the home button twice and swipe the application out of the screen, finalizing it, WillTerminate will be called and after that, the app DIED, I can't do anything more.
On the Android I can do it and keep the app running, without show it on the android's app switcher.
There's a way to do it?
And how I re-open the app every time I kill the app (Every time WillTerminate is called).
You cannot avoid the kill by the user. Apple philosophy is that the user is the "commander-in-chief" and he can decide to kill your app when he wants (and the kill must be real, no hidden processes).
All background tasks of the app will terminate with it and you cannot reverse the user decision (i.e. restarting automatically).
I don't see simple solution for your problem. The only thing that I can think is to put a big alert message to the user, when a specific task begins, saying:
"don't kill this app from the switcher while this operation is running ..."
or, save the state of the background process and restart it at the next run of the app.
How can I detect if the Application has been idle for let's say 30 seconds?
I know this is possible by using a DispatcherTimer and then restarting it at PhoneApplicationPage.ManipulationCompleted event? But, I am concerned as this will affect the performance of the application.
Are there any better solutions?
You're on the right track. There isn't an explicit "idle" notification (especially not one that fast).
ManipulationCompleted may not always fire for you since other input can prevent the manipulation from starting and a user could do a very long manipulation. I'd reset the timer on any mouse input rather than just on ManipulationCompleted.
Depending on how exact you need your 30 second timer to be I would consider leaving the timer running and setting a flag for the last input. When the timer expires then check if the flag has been set. This way you won't need to continuously reset the timer for every user input.