locked
IBackGround Task Update UI RRS feed

  • Question

  • perhaps I acting in the wrong direction

    Remote activating APP, this includes a Service (IbackgroundTask) purpose is ongoing communication between caller and service.

    The service must refresh own UI. -???

    How to get the instance of the UI, especially the tpye of page is not available in the Ibackgroundtask class?

    thanks for an help

    Thursday, November 16, 2017 8:09 AM

Answers

  • Hi HannesPreishuber

    You want to update UI when the out-of-process background task is triggered, am I right?

    You could create event handlers to handle completed background tasks or background task progress. Then in the handlers, use Dispatcher.RunAsync() method to update UI. Not in the Background Task Class.

    You could refer the following code from the background task sample:

          

      /// <summary>
            /// Attach progress and completed handers to a background task.
            /// </summary>
            /// <param name="task">The task to attach progress and completed handlers to.</param>
            private void AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration task)
            {
                task.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
                task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
            }
     
            /// <summary>
            /// Handle background task progress.
            /// </summary>
            /// <param name="task">The task that is reporting progress.</param>
            /// <param name="e">Arguments of the progress report.</param>
            private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
            {
                var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    var progress = "Progress: " + args.Progress + "%";
                    BackgroundTaskSample.SampleBackgroundTaskProgress = progress;
                    UpdateUI();
                });
            }
    /// <summary>
            /// Update the scenario UI.
            /// </summary>
            private async void UpdateUI()
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () =>
                {
                    RegisterButton.IsEnabled = !BackgroundTaskSample.SampleBackgroundTaskRegistered;
                    UnregisterButton.IsEnabled = BackgroundTaskSample.SampleBackgroundTaskRegistered;
                    Progress.Text = BackgroundTaskSample.SampleBackgroundTaskProgress;
                    Status.Text = BackgroundTaskSample.GetBackgroundTaskStatus(BackgroundTaskSample.SampleBackgroundTaskName);
                });
            }

    For more information, please refer Monitor background task progress and completion.

    There is also a sample about background task which implement updating UI in the background task in Scenario1.

    You could download it here: BackgroundTask.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 17, 2017 8:07 AM