Event fire in Windows Runtime Component (WinRT) background task not being propagated to UWP subscribers

Marco Enxuto 326 Reputation points

I have a strange issue with Background tasks using WinRT.
I have an app that has a background task which is implemented as out of process pattern.

This background task is a Windows Runtime Component.
I would like to subscribe an event from the main app (UWP) because if the background task runs by a push trigger i want the app to be notified and do some actions like, locking the UI.

This looks very trivial but there isn't.
I read the documentation here and it looks like we have 3 options to follow.
I chose the first 2, and even implementing that way, subscribers of my event in the main app do not get triggered.

From now on consider the eventname DownloadStarted;

Subscribing from the main app to events on the BackgroundTask that contain this delegates BackgroundTaskCompletedEventHandler and BackgroundTaskProgressEventHandler it works.
By using a custom delegate, it raises an error on compilation. WinRT does not support Nested types.
So, the only way is by following the document I mentioned above, I guess.

I implemented using the EventHandler and I raise the event when the code Runs on the Task class.
By debugging, it looks like the no one subscribed DownloadStarted because it appears null.
But if i do subscribe inside the Task class in the backgroundtask, it works as shown below.

What I'm doing wrong?

See the code example below:

Operation Class (Helper class to be subscribed by Page_Load on Main App)

using System;  
namespace TestApp.BackgroundTask  
    public sealed class Operation  
        // Declare the event  
        public event EventHandler<object> DownloadStarted;  
        // Raise the event  
        private void OnDownloadStarted()  
            if (DownloadStarted != null)  
                DownloadStarted(this, null);  


Task (main BackgroundTask)

//Code removed for brevity  
public void Run(IBackgroundTaskInstance taskInstance)  
     Operation operation = new Operation();  

Page Main (UWP App)

//Code omitted for brevity  
private void Page_Load(object sender, RoutedEventArgs e)  
    Operation operation = new Operation();  
    operation.DownloadStarted += Operation_DownloadStarted;  
Universal Windows Platform (UWP)
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,936 questions
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points

    Welcome to Microsoft Q&A!

    As Viorel-1 said you should not create two different Operation objects and even if you use static class or singleton pattern, it will not triggered as expect also. Because it's out- process model. and it will never generate same instance at same time.

    . I get events fired to the main app. I don't understand why does not work with a custom EventHandler declared in the BackgroundTask.

    Completed is even of IBackgroundTaskRegistration, it is not same as the backgroundtask that inherit IBackgroundTask, they are two interfaces. and Completed is process-communication, but the custom events is thread-communication.

    For your scenario, the better way is use LocalSettings to pass data or using Progress property that could trigger Progress event. For more please refer to official code sample

    Thank you.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful