Background Task In Universal Windows Program - Part Seven

Before reading this article, please go through the following articles:

This article explains about Host Appservice into the single process system, like Single Background process, as we have seen in the Background Task in Universal Windows Program - Part Four.

diagram

Appservice into a single process implementation diagram.

This feature is available in Windows 10 Anniversary Update (Version 1607). To implement this, two changes must be implemented.

  1. Entry point is not required because the default entry point handles this in the main application.

    Package.appxmanifest file declares only the Appservice Name.

    Appservice

  2. Override the OnBackgroundActivated function and implement the Appservice code with the sample code.
    1. protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) {  
    2.     base.OnBackgroundActivated(args);  
    3.     RunBackGroundTask(args);  
    4. }  
    Background Task in UWP - Part six sample Windows Runtime component -- Appservice code has moved into our main application RunBackGroundTask function. The code will look like the following:
    1. private void RunBackGroundTask(IBackgroundActivatedEventArgs args) {  
    2.     _taskInstance = args.TaskInstance;  
    3.     _taskInstance.Canceled += TaskInstance_Canceled;  
    4.     _bgTaskDeferral = _taskInstance.GetDeferral();  
    5.     var triggerAppService = _taskInstance.TriggerDetails as AppServiceTriggerDetails;  
    6.     if (triggerAppService == nullreturn;  
    7.     _appServiceConnection = triggerAppService.AppServiceConnection;  
    8.     _appServiceConnection.RequestReceived += AppServiceRequestReceived;  
    9.     _appServiceConnection.ServiceClosed += _appServiceConnection_ServiceClosed;  
    10. }  
    11. private async void AppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) {  
    12.     var serviceMsg = args.GetDeferral();  
    13.     serviceMsg.Complete();  
    14. }  
    15. private void _appServiceConnection_ServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args) {  
    16.     _bgTaskDeferral.Complete();  
    17. }  

Build the application. That’s it. Now, Appservice has been hosted into the main Application.

Example: Will takethe same example in Background Task in Universal Windows Program - Part Six and host it into the main application.

Steps

Main Application changes

  1. Create a blank application & implement OnBackgroundActivated function like the above steps.
  2. Appservice RequestReceived function implements handling the command part and there's no need to implement the set command implementation because our main application never communicates to the Appservice. Only the outside of another application will communicate. So, only get the command that has to be implemented.
    1. private async void AppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) {  
    2.     var serviceMsg = args.GetDeferral();  
    3.     var storeData = args.Request.Message;  
    4.     string command = storeData["Command"as string;  
    5.     ValueSet returnData = new ValueSet();  
    6.     if (command == "set"//Update information in the ApplicationData  
    7.     {  
    8.         string inventoryIndex = storeData["serverMsg"as string;  
    9.         ApplicationData.Current.RoamingSettings.Values["set"] = inventoryIndex;  
    10.         returnData.Add("Result""OK");  
    11.         await args.Request.SendResponseAsync(returnData);  
    12.     } else if (command == "get"//Read information from the ApplicationData  
    13.     {  
    14.         string inventoryIndex = (string) ApplicationData.Current.RoamingSettings.Values["set"];  
    15.         returnData.Add("Result", inventoryIndex);  
    16.         await args.Request.SendResponseAsync(returnData);  
    17.     }  
    18.     serviceMsg.Complete();  
    19. }  
  3. No need to implement Appservice communication part.
  4. Textbox enter key event directly assigns the values into the ApplicationData (in the Part 6 example using Appservice we assigned this data)
    1. private void TxtStatus_OnKeyDown(object sender, KeyRoutedEventArgs e) {  
    2.     if (e.Key != VirtualKey.Enter) return;  
    3.     TxtSendStatus.Text = "Send Msg";  
    4.     var sendMsg = TxtStatus.Text;  
    5.     ApplicationData.Current.RoamingSettings.Values["set"] = sendMsg;  
    6. }  

Client UI Communicate to Appservice

This application only changes, AppserviceName and PackageFamilyName. That’s it.

private const string AppServiceName = "SingleAppService";
private const string PackageFamilyName = "b6512b79-4bc1-4b5a-a8d7-86c0c74aa169_kz0gnaa3h8516";


Output

Output

  1. In Main Application, on each enter key event, the data is assigned to the ApplicationData class.
  2. Whenever client requests the Appservice, Appservice reads the data from the ApplicationData class and sends it to the request UI.

Note:

  1. Main Application is running in the foreground and another client has requested to Appservice. Appservice is activated to handle the client request without affecting the main application task.

    task

  2. Main Application is not running in the foreground, Run the client application. Now, the Appservice will start responding to the UI because Appservice is running into the System Background apps (Appservice is hosted intoa single process (Main application) and the system is automatically registered in System Background apps).

    apps

Complete Source code is available on GitHub.

Up Next
    Ebook Download
    View all
    Learn
    View all