2
votes

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.

2

2 Answers

0
votes

if your are just starting out use Windows Template Studio it will be perfect for you it is an extension of visual studio which lets u create new uwp projects with a lot of built in features, and u can only choose the features you want, will save u a lot of time on basic stuff. https://github.com/Microsoft/WindowsTemplateStudio

0
votes

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.

  1. 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.

  2. 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://docs.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!