Join Scott Peterson for an in-depth discussion in this video Adding and configuring a background task, part of Developing UWP Apps: 9 Background Tasks.
- [Instructor] Okay, we're over here in Visual Studio 2017 in our tagur app we've been working on in this series. And you may notice by looking at the Solution Explorer that I've made a number of changes to the solution. Number one, because we're not going to be using it in this session and we don't want to get confused in the Solution Explorer, I've removed the Windows runtime component that handled audio effects and I've split our solution now into two projects. We've got the main project tagur, and I've moved much of the core functionality to tagur.Core. This gives us the ability to communicate with our view model, models et cetera, from a background task more easily.
Let's go ahead and run our tagur app and take a look at what we've got and where we're adding this series just to make sure we're all on the same page. So you'll notice through the previous sessions we've worked through elements like layout, navigation, adaptive triggers, visual states. We've taken a look at advanced controls, commands, converters, push notifications, networking, storage, and we have a fairly full functioning app including the ability now to store our information in a connected cloud service using Azure Blob storage.
So that being said, let's see how we might add some background task functionality to our app. So we'll go ahead and stop that solution. And if you remember the steps, our first step is to add a Windows Runtime Component for a background task. So we'll say Add, New Project, and we'll select Windows Runtime Component, and we're going to call this tagur.Services.Background. Make sure our namespace stays consistent. We'll just select the defaults.
And you'll notice by default it's actually creating a public sealed class for us. We can delete that and create a new class or in Visual Studio 2017, we can simply do a rename. And I'm going to call this Listener. It's not necessarily the best name, but it's going to serve our purposes here. We'll just call this Listener, and we'll refactor that. Now we have our public sealed class Listener. We need to now implement the IBackgroundTask interface. So when we say IBackgroundTask, it's going to say wait a minute I don't understand what that is.
We'll say using, and then I want you to notice that it's still saying that we have an issue. It says Listener does not implement the interface IBackgroundTask run Because it's implementing that, we need to add the run. We can simply say implement interface, and there we go. Now we have the shell for our background task. We haven't added all the code there but we'll come back to that in a second. The next step is to set project references. I'm going to do a couple things here.
In my background task, I'm actually going to add a reference to that core project so that we can have access to the models and extension methods and view models and things like that. But for the background task the important reference is from our primary project, we need to add a reference to tagur.Services.Background. And there we go. And then the next step is to go to our package manifest and declare the background task as an available declaration.
So we'll say Background Task, Add. And for this first background task, we're just going to use a system event and a timer. And we could have multiple types that this background task supports. We're going to look at push notification later, but this could even be based on a location change, so if a user changes their location within a certain geo-fence, for example, if you kick off this background task. A lot of different opportunities here for task types. But now we need to add an entry point. So for the entry point, we need to give the fully qualified name of the class itself.
So you'll remember that this is tagur.Services.Background and then the class name is Listener. So we simply add in the entry point tagur.Services.Background.Listener and now we're connected to that background task. We'll just go ahead and do a build on that. Okay, great, now we're going to be adding the code to register the task a little bit later on in this session but I do want to add the cleanup code here just sort of as a placeholder here.
Let's take a look at the two things we actually care about in our background task. Number one is the task instance. The task instance is going to give us the ability to take a look at things that come through related to the task, like the name of the task and any parameters that come through the task. And then the deferral is going to give us the ability to clean it up and to complete the task. So background tasks can only run for a short period of time. Typically this is less than 30 seconds or so although it's not really a hard and fast rule.
Sometimes it's based on memory. But we need to basically grab the deferral from the task instance and then tell the operating system when we're complete. So what we'll be doing here is grabbing the deferral and then processing any code and then calling deferral.Complete. Now there's other things we can do in here. We can handle errors, trap those if we need to, and perform some additional cleanup as well, but it's not necessary at this point to handle any of that because we're going to be looking at a fairly simplified scenario. So that's it, we've got our background task implemented and configured.
Let's flip back over to the slide deck and take a look at what it might take to implement a system event or timer-based background task now that we have our shell created.
Note: This course was created by Wintellect. We are pleased to host this training in our library.