Search code examples
c#uwpevent-handlingbackground-taskuwp-app-service

How to get notification from UWP app service in the main app?


I implement UWP app service (windows runtime component) as described here. It is working fine and as far as I understand it is out-of-process background task by its nature.

What I need: to get notification in the main app when

private void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{

}

is finished (when my background task is completed). I tried static event/event handler but when I set event handler from the main app, it is null during background task execution.

Another thing I tried is to subscribe to Completed event of background app from the main app. But BackgroundTaskRegistration.AllTasks is empty in the main app, should I additionally register uwp app service as background task? Now it is specified in the manifest with name/entry point.


Solution

  • You can convert your out-of-process model App Service into an in-process model, and handle task complete event in App.xaml.cs .

    You can refer to this document Convert an app service to run in the same process as its host app.

    Remove the EntryPoint attribute from the element because now OnBackgroundActivated() is the entry point that will be used when the app service is invoked.

    Move the service logic from its separate background task project into methods that can be called from OnBackgroundActivated().