locked
How to Return Results from background tasks RRS feed

  • Question

  • I have a time triggered background task which will do some work and has to return some list of objects to the UI thread.

    I have registered the task from the main UI and also have the completed handler defined. How do I set the results when background task is complete and also how do I trigger the Completed event handler.

     task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);

     public void Run(IBackgroundTaskInstance taskInstance)
            {
                this.backgroundTaskDeferral = taskInstance.GetDeferral(); // Get a deferral so that the service isn't terminated.
                taskInstance.Canceled += OnTaskCanceled; // Associate a cancellation handler with the background task.

                ...Do Work();

                 //How to set the result and trigger Completed event handler?


            }

    I do not see any sample which returns any result.Please help

    Wednesday, February 24, 2016 7:55 PM

Answers

  • Write the results to disk and use a file watcher that fires when a change occurs.

    http://pauliom.wordpress.com

    • Marked as answer by Laksjay Monday, February 29, 2016 7:48 PM
    Wednesday, February 24, 2016 8:00 PM

All replies

  • Write the results to disk and use a file watcher that fires when a change occurs.

    http://pauliom.wordpress.com

    • Marked as answer by Laksjay Monday, February 29, 2016 7:48 PM
    Wednesday, February 24, 2016 8:00 PM
  • Then I need to start worrying about the file management etc. 

    What I need is simple mechanism to pass the result as args to the OnCompleted method.

    Wednesday, February 24, 2016 9:40 PM
  • Hi Laksjay,

    Since the BackgroundTaskCompletedEventArgs only contains InstanceId property, cannot pass your result through args.Reference you can see https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(Windows.ApplicationModel.Background.BackgroundTaskCompletedEventArgs);k(TargetFrameworkMoniker-.NETCore,Version%3Dv5.0);k(DevLang-csharp)&rd=true

    So, one way is as @pkr2000 mentioned. Another way  I recommend you to use App service.This is  two way communication mode. Client can get the background task result by register RequestReceived event.Details about app service you can see:https://msdn.microsoft.com/en-us/library/windows/apps/mt187314.aspx. Samples you can see https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/AppServices

     

    Best Regards,
    Sunteen Wu


    • Edited by Sunteen Wu Thursday, February 25, 2016 2:29 AM
    Thursday, February 25, 2016 2:28 AM
  • Thanks for your reply. If I have a app service which has time triggered background task, I understand that, the main app can communicate with the appservice by adding OnRequestReceived and exchanging request/response. But if I have time triggered task in my app service, how do I communicate with the main app that task is completed and pass the result.I mean the reverse scenario where appservice needs to communicate to main app.
    Thursday, February 25, 2016 5:35 PM
  • After doWork () call the complete method

    like this.BackgroundTaskDeferral.Complete(); // Task Completed. 


    Vinoth Rajendran

    Thursday, February 25, 2016 5:51 PM
  • It is not about calling the completed handler. How do I pass results back to the Completed Handler?
    Thursday, February 25, 2016 6:04 PM
  • Windows Runtime component

    this.BackgroundTaskDeferral.Complete();

    MainApplication implement

    Implement event handler in  BackgroundTaskRegistration

    ex : sample code

     var regBackTask = new BackgroundTaskBuilder
                    {
                        Name = "MyTask",
                        TaskEntryPoint = TaskEntryPoint //Your task entry point
                    };

    BackgroundTaskRegistration taskRegistration = regBackTask.Register();
       taskRegistration.Completed += TaskRegistration_Completed; //Implement event handler


    Vinoth Rajendran


    Thursday, February 25, 2016 7:21 PM
  • This is not the problem I am trying to resolve.I understand how to set the Completed handler. The question is how do I pass the data to the completed handler from the background task. 

    I have a list of objects which I need to pass back to the Completed handler. 

                            

     private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
            {


                // retrieve the list of Objects and display

            }

    BackgroundTaskCompletedEventArgs args are sent back. How do I send the list of objects in this BackgroundTaskCompletedEventArgs. 

     public async void Run(IBackgroundTaskInstance taskInstance)
            {
                this.backgroundTaskDeferral = taskInstance.GetDeferral(); // Get a deferral so that the service isn't terminated.
                taskInstance.Canceled += OnTaskCanceled; // Associate a cancellation handler with the background task.

                doWork();

                 How do I set the list of Objects? I tried Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();But I am not able to set the list of Objects in this.This is the problem I am trying to resolve

                backgroundTaskDeferral.Complete();



            }

    Thursday, February 25, 2016 8:19 PM
  • Thanks for your reply. I think that was the only solution I could use to share the data from background task with the GUI
    Monday, February 29, 2016 7:49 PM