Background Task In Universal Windows Program - Part Six

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

This article is in continuation of part five of the series of articles and explains how communication happens between two apps, with examples.

In the below sample application, one app sends the information to AppService and the another app reads the information from the AppService. The following diagram explains about the steps.

diagram

Two apps are having the same procedure except the command. The first one sends the set (write) while the second app gets (read) request. For better AppService communication, we will create a common class library which both the apps will share.

Note: Creation of AppService communication has been explained in Part 5.

Create a Class Library

  1. Create a Class Library Project.

    Class Library Project

  2. Class Library Implementation.
    1. public static class AppServiceManager  
    2.     {  
    3.         private static AppServiceConnection _appService;  
    4.         private static AppServiceConnectionStatus _appStatus;  
    5.   
    6.         public static async Task<bool> OpenAsync(string serviceName,string packageName)  
    7.         {  
    8.             _appService = new AppServiceConnection  
    9.             {  
    10.                 AppServiceName = serviceName,  
    11.                 PackageFamilyName = packageName  
    12.             };  
    13.   
    14.             _appStatus = await _appService.OpenAsync();  
    15.   
    16.             return _appStatus == AppServiceConnectionStatus.Success;  
    17.         }  
    18.   
    19.         public static async Task<string> SendMessageAsync(string command,string message)  
    20.         {  
    21.             if(_appStatus != AppServiceConnectionStatus.Success)  
    22.                 throw new Exception("AppService has failed!!!");  
    23.   
    24.             var responseUpdate = new ValueSet();  
    25.             responseUpdate.Add("Command", command);  
    26.             responseUpdate.Add("serverMsg", message);  
    27.   
    28.             var response = await _appService.SendMessageAsync(responseUpdate);  
    29.             var result = response.Message["Result"as object;  
    30.             return (string)result;  
    31.         }  
    32.   
    33.     }  

Build the project

Main Application Implementation with AppService Registration.

  1. Create a write application ( explained in Part 5) with AppService Registration.
  2. Add Class Library reference to communicate the AppService.

    Add Class Library Reference

  3. Implement the Connection and Command.
    1. private const string AppServiceName = "com.microsoft.BgAppService";  
    2. private const string PackageFamilyName = "69533c06-5ce5-4f1a-93fe-151ca737537a_kz0gnaa3h8516";  
    3.   
    4.   
    5.   
    6. private async void OpenService()  
    7.         {  
    8.             await AppServiceManager.OpenAsync(AppServiceName, PackageFamilyName);  
    9.         }  
    10.   
    11.         public async void CreateService(string sendMsg)  
    12.         {  
    13.             var textMsg = await AppServiceManager.SendMessageAsync("set", sendMsg);  
    14.             TxtSendStatus.Text = textMsg;  
    15.         }  
    16. Textbox is entering event, Send the message to Service  
    17. private void TxtStatus_OnKeyDown(object sender, KeyRoutedEventArgs e)  
    18.         {  
    19.             if (e.Key != VirtualKey.Enter) return;  
    20.             TxtSendStatus.Text = "Send Msg";  
    21.             var sendMsg = TxtStatus.Text;  
    22.             CreateService(sendMsg);  
    23.         }  
    24. Service code Command Handling  
    25. private async void AppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)  
    26.         {  
    27.             var serviceMsg = args.GetDeferral();  
    28.   
    29.             var storeData = args.Request.Message;  
    30.             string command = storeData["Command"as string;  
    31.               
    32.   
    33.             ValueSet returnData = new ValueSet();  
    34.   
    35.             if (command == "set"//Update information in the ApplicationData  
    36.             {  
    37.                 string inventoryIndex = storeData["serverMsg"as string;  
    38.                 ApplicationData.Current.RoamingSettings.Values["set"] = inventoryIndex;  
    39.                 returnData.Add("Result""OK");  
    40.                 await args.Request.SendResponseAsync(returnData);  
    41.             }  
    42.             else if (command == "get"//Read information from the ApplicationData  
    43.             {  
    44.                 string inventoryIndex = (string) ApplicationData.Current.RoamingSettings.Values["set"];  
    45.                 returnData.Add("Result", inventoryIndex);  
    46.                 await args.Request.SendResponseAsync(returnData);  
    47.             }  
    48.   
    49.             serviceMsg.Complete();  
    50.         }  

Another Client reads from the Service.

  1. Create a Blank App (Universal Windows).

    Create a Blank App

  2. RequestService gets the message (Add reference ServiceManager dll).
    1. public async void RequestService()  
    2.         {  
    3.             try  
    4.             {  
    5.                 await AppServiceManager.OpenAsync(AppServiceName, PackageFamilyName);  
    6.   
    7.                 RecvBlock.Text = await AppServiceManager.SendMessageAsync("get""");  
    8.             }  
    9.             catch (Exception exception)  
    10.             {  
    11.   
    12.             }  
    13.         }  
  3. Implement DispatcherTimer and read the information each second, from the AppService.
    1. var dispatcherTimer = new DispatcherTimer();  
    2. dispatcherTimer.Tick += DispatcherTimer_Tick;  
    3. dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);  
    4. dispatcherTimer.Start();  
    5.   
    6.   
    7. private async void DispatcherTimer_Tick(object sender, object e)  
    8.         {  
    9.             await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, RequestService);  
    10.  }  

Build & Deploy the Project

Output

Run the two apps at the same time. One is for sending while another one is for receiving the information. 

Output

Overview this sample

  1. BGAppService: Running the AppService.
  2. ServiceManager: Class Library for communication to the BGAppService
  3. MainApplication: Registers BGAppService and sends the information to the BGAppService using the ServiceManager Library.
  4. ClientUI: Using the ServiceManager Library reads the information from the BGAppService, using DispatchTimer.

Up Next
    Ebook Download
    View all
    Learn
    View all