At the moment I want to register a background task. Here is the registration code:
private void RegisterBackgroundTask() { var exampleTaskName = "BackgroundTaskForMedia"; foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == exampleTaskName) { Debug.WriteLine("Unregister"); task.Value.Unregister(true); break; } } BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder(); taskBuilder.Name = taskName; taskBuilder.TaskEntryPoint = taskEntryPoint; taskBuilder.SetTrigger(new TimeTrigger(15,false)); var registration = taskBuilder.Register(); } And here is the code of this background task (purely an example that would just understand whether it works or not):
public async void Run(IBackgroundTaskInstance taskInstance) { _deferral = taskInstance.GetDeferral(); Debug.WriteLine("In BackgroundTaskForMedia.Run"); await Open(); _deferral.Complete(); } private async Task Open() { await Launcher.LaunchUriAsync(_uri); } So, when you start the application, this task appears to be registered, because after a new call to the registration code, a message is displayed on the console to deregister.
However, the task itself is not executed. A new page in the browser does not open, nothing new on the Debug panel. What's wrong?