-1

I'm working on a UWP project and recently I've added an out of process background task to it. I register the task in the OnLaunched() method in App.cs in order the task to be run only once the app is launched. But there is a problem: I have to refresh my xaml view after the task is completed and I have absolutely no idea how to make it happen(if I move the registration of the task to the Main.xaml.cs, it will be run every time the page is opened). I'll be happy to get any kind of help from you. Here is my StartBackgroundTask() method which I call in the OnLaunched() method:

private async void StartBackgroundTask()
    {
        var task = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault(x => x.Name == "testTask");
        if (task != null)
        {
            task.Unregister(true);
        }

        var taskBuilder = new BackgroundTaskBuilder();
        taskBuilder.Name = "testTask";
        taskBuilder.TaskEntryPoint = typeof(UpdateLocalDbBackgroundTask.UpdateLocalDbBackgroundTask).ToString();

        ApplicationTrigger trigger = new ApplicationTrigger();

        taskBuilder.SetTrigger(trigger);
        taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
        task = taskBuilder.Register();            

        await trigger.RequestAsync();
    }
  • May not be what you are looking for so I won't put it as an answer, but you could just add a public static bool to a class in your project and have it be set to true by default. Then before you register your task, check to see if the bool is true (which it will be by default on start). As soon as you register your task one time, set it to false so it never runs again. Example of global class/variable usage: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/3838d93a-b74a-4db3-b030-e88ceed39d31/uwp-how-to-pass-values-between-two-pages – Bmils Apr 09 '21 at 18:08
  • I like that idea, you could for example also use the `ApplicationData.Current.LocalSettings.Values` dictionary and then have an async method on the `MainPage` check for a change in a value you set when you're done with the update method in `App.cs`. – Leander Apr 09 '21 at 19:43
  • Details on how to correctly use `BackgroundWorker` can be found in the answers to the duplicate question, including how to subscribe to the `RunWorkerCompleted` event with a handler. Of course, the documentation also has a wealth of information. If after reviewing those available references you still have problems, feel free to post a question that includes a proper [mcve] along with a detailed explanation of what that code does, how that's different from what you want, and what _specifically_ you need help with. – Peter Duniho Apr 21 '21 at 06:31

1 Answers1

0

You could create an event on the App.cs and handle it in the target XAML page. Let's say it's MainPage. Then you could call the event after you registered the background task.

Here is a very simple code sample:

App.cs

    public static event Action<String> TaskCompleted;

    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }


    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        //some other code
         Window.Current.Activate();
        }

        App.TaskCompleted?.Invoke("Task Complete");

    }

MainPage.cs

    public MainPage()
    {
        this.InitializeComponent();

        App.TaskCompleted += App_TaskCompleted;
    }

    private void App_TaskCompleted(string obj)
    {
        Debug.WriteLine(obj);
    }
Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13