I just started learning UWP and i'm really confused on how it works. I already saw tens of posts that talk about my problem but can't figure out how to do what I want.
So I want to make an app that runs on windows startup, I want the app to be not visible so it needs a background Task, how can I trigger this background task without getting to the app UI ?
The app is supposed to have the Background Task always running, and its interface is supposed to be used as "settings" so I don't need the app to be shown on startup.
Thanks.
I found my way here after a lot of googling. to be honest I have come to the same conclusion as Motaz. But as of writing this I am way too invested in what I have already. While what I have here is not the perfect answer to his question. I wanted to come back and post what I've learned for anyone else who ends up here.
My need is a app that when started will monitor a third party USB device until the app is closed (regardless of whether the app is minimized or not)
Windows Template Studio is good, but the docs not so much. Especially when it comes to Background tasks.
I started here: https://github.com/Microsoft/Windows-universal-samples. But there is two problems.
They combined all the examples and some of the code is shared, which makes it difficult to pull a project out and hack it apart without breaking the original examples.
Following their background task example I perpetually had issue with the manifest and and it wanting an audio task
I went back to the template studio and created the simplest version with a background task possible. After a lot of trial and error I got something that works. I have posted it here: https://github.com/PillarOfSociety/WindowsTemplateStudio-BackgroundTask
Some things to note:
I am no expert on UWP and while this runs I have no intent on putting it in the store nor did I try.
If you do download my project and run it, you should be able to just hit the "Add events" and the "Start Back" button and the task should run and update the Progress bar.
I used an ApplicationTrigger. the original example from the template uses a TimeTrigger which takes time in MINUTES (took me too long to figure that out). Supposedly Application triggers have a 30sec timeout.. but on my laptop they live for much longer.. I don't know how long. This page is useful: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/support-your-app-with-background-tasks
The template studio generated BackgroundTaskService will leave background tasks registered after the app is closed, but will NOT make the connection back to them once its rerun, so on a rerun either the task appears not to run, or will crash the app when triggered.
Important Code I discovered:
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
TestText += task.Value.Name; //gets the typename of the task
//task.Value.Unregister(true); //will unregister tasks
}
await Task.CompletedTask;
The tasks in BackgroundTaskRegistration.AllTasks are not the BackgroundTask class that the template studio uses. IN my example I unregister all of them each time it runs so that you have a reference to the task as an instance of BackgroundTask.
Hopefully this helps someone and Good luck!
If you're just starting out use Windows Template Studio, it will be perfect for you. It is an extension of Visual Studio which lets you create new uwp projects with a lot of built in features, and you can only choose the features you want. It will save you a lot of time on basic stuff.
https://github.com/Microsoft/WindowsTemplateStudio
Related
In our app users can track and submit journeys they have recorded. I need a simple way of creating a task in iOS. I have already created and tested this on Android. It works via:
The user selects the journeys they would like to submit.
Taps sync and a foreground service is created that syncs the journeys to our API.
This service will continue to sync journeys even if the app is put into the background or even closed.
So in short how can i achieve this on iOS 9-13?
I have already tried creating a background-safe tasks using:
nint taskID = UIApplication.SharedApplication.BeginBackgroundTask( () => {});
However, this only gives the task 2 mins to run which isn't enough. I have looked into the NSURlSessions but they require URls, whereas we are using our own API wrapper.
I would simply like a way of creating a task when the user taps 'sync' and this task also being capable to run in the background. I am not too bothered if the task is cancelled when the app is closed, although if possible would like the task to continue.
Thanks.
This service will continue to sync journeys even if the app is put
into the background or even closed.
First, if your app is closed in iOS, I'm sure do can't run any service in background.
Then if your app is put into background, Apple has strict limit to allow apps running in the background. background-tasks has time limits, you can read the document about more information. There is a section about handling-background-task-time-limits which you can have a try.
Also, Apple allows some specific apps to run in background which have to perform tasks in the background. For example, app that needs to play music in background, update location in background and etc. You can see the Application Registration Categories here. If your app meets the requirement there, you can apply for a background running permission from system.
Refer: Backgrounding in Xamarin.iOS
I would advise you to leverage on Shiny to achieve it.
PerformFetch is the closest thing to what you ask for, it will run in the background and update your app when iOS thinks it is needed (it predicts that according to the previous behavior the user will soon open your app and that the new content is available).
The only alternative is to send the push notification when you want the app to be updated.
That's about it, I understand your wish but it is just that - a wish and not something that can be real.
I am using a 3rd party rest api to query data and display it in my app. I have to perform a task like at night 12 approx. it will perform a background task to query data from rest api and update live tile and generate notification. I would like to use only C# only for this task. I don't know what will be best approach to do this task. But I using below code to perform background task to do this which is not working. Not sure why?
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = taskName;
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.TaskEntryPoint = typeof(BackgroundTask.BackgroundTask).FullName;
taskBuilder.Register();
and from background task I am querying data and generating toast notification.
Any help why this code is not working or when this task will fire. Is there any better approach to do above task?
Thanks
Regarding the code you have not working...
For Windows Phone 8.1 unlike Windows 8\8.1, you are required to call BackgroundExecutionManager.RequestAccessAsync() (search MSDN\internet) for ANY background task before registering task(s) whereas in Windows this is only required for some tasks. Make sure your code calls this and validate the returned value before registering your background task(s).
Regarding knowing if your task "worked"...
It's a good idea to have the background task implementation run (IBackgroundTask::Run()) independent of the trigger\conditions you've set to ensure it performs without issue by debugging it. See instructions in the following link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/jj542416.aspx.
Regarding your use of SystemConditionType.InternetAvailable...
I'm not 100% about this but I'm pretty certain this will be redundant given you already have a SystemTriggerType.InternetAvailable. I don't know of a situation where the trigger would fire but the condition wouldn't be true.
Regarding the requirement you've mentioned...
If I understand your requirement correctly you have different options here:
If your app is a Windows Phone XAML app that need to run based on time, I would recommend either TimeTrigger or MaintenanceTrigger triggers (as opposed to the SystemTrigger). These are both Background Tasks. For general info on Background Tasks and links to the TimeTrigger and MaintenanceTrigger documentation see this MSDN link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh977056.aspx.
If your app is a Windows Phone Silverlight 8.0 app you can use Background Agents, specifically either PeriodicTask or ResourceIntensiveTask. See the links posted by others or search the MSDN\internet for more info.
If your app is a Windows Phone Silverlight 8.1 app you can use the option in either 1 or 2 above.
I think you should try using PeriodicTask. Also consider the constraints mentioned in the link.
create one class with output Type :Windows Runtime Component
and put your Class that inheritance from IBackroundTask so this work
if you use from emulator for launching app, i think your app for register task not active in emulator.
I have a requirement where I have to call a service in background after every let say 1 hour to get some informations from server. I am working on JavaScript Metro Application. I have tried the background task and used Time Trigger and I have scheduled it to get triggered in every 15 minutes. It get called first time and then it is never called. I didn't close the background task because I want it to run all the time and call the service at scheduled time.
I have used the Microsoft Background task sample for reference.
Please tell me what should be the best approach to call a service in background.
How to use Time Trigger and Why Time Trigger doesn't get called after first time?
Please share code sample or walkthrough if any.
Thanks
First thing you should do is to close the background task properly as instructed in the documentation - if your tasks don't behave nicely, platform might suspend and refuse to run them for some time. You should let the platform handle triggering of the events based on the triggers and conditions you define instead of trying to bend the system. Also, remember that there's CPU and data usage quotas for background tasks present, one can't do massive amount of processing in background tasks - if the quotas are exceeded, tasks will get suspended. Be also sure that the background task works and doesn't throw errors.
In general, my recommendation is that one shouldn't rely solely on background tasks to fetch the information since it's not guaranteed that they manage to do it on time, so better to prepare for downloading the needed data in the foreground app as well. This obviously depends on the use case: if the data fetched in background tasks is not critical but more like nice-to-have, there's much less to worry about.
The TimeTrigger requires the app being added to the lock screen (see docs), but I guess you already meet this requirement since you've managed to get the task running once.
For debugging the background tasks, please take a look at Event Viewer, see detailed instructions. That page also contains some tips about common problems. The Event Viewer entry mentioned in that document is often a valuable resource for figuring out problems with the bg task execution. My guess is that you'll see errors there related to not closing the task properly.
I've got code that uses the C# TaskManager object to create a task. On Windows 7 it works fine but on Windows XP (and presumably other Windows) it doesn't work at all because the default user for the task is system and thus there's no session for the GUI to be displayed. If I modify the created task manually in the control panel widget to set the job to run only when user is logged in and only for the particular user, then everything works perfectly. But despite hours of searching I see no options for setting these options in the C# objects. Anyone know a solution with the existing objects? I'd hate to rewrite everything to manually run the scheduler EXE and pass in stuff by command-line.
Q
Okay, I figured out the answer!
I didn't realize it but I had been using a third-party Task Scheduler Managed Wrapper (it'd been a while since I wrote that part of my code) and that explains why help was hard to find! I stumbled across that page a moment ago and right there in their examples was just what I needed! The detailed solution in context can be found here, but the key part is:
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.Principal.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
td.Principal.LogonType = TaskLogonType.InteractiveToken;
Thanks for trying to help!
This program should not have a visible window, more like a daemon. (a taskbar program for instance) Any links or advice will be appreciated.
You might want to consider writing a Windows Service.
If you just need to execute a specific action at certain times you could make use of the Windows Task Scheduler.
If you want an icon in the system tray you could write an ordinary Windows Forms application but have the main form hidden until the user requests it. This article explains how.
Or if you want to run tasks on a timer, the Windows Task Scheduler's where it's at.
When I've built applications that need this functionality before, I've always used the Windows Task Scheduler through a simple .NET library that I found.
Please see my answer to a similar question for some sample code and more explanation.