Trigger The Background Task From GUI

Introduction

In Background Task, generally invoked by one of the systems, devices, audios, toasts, network related events or other (check MSDN more info about the BG task events) events, basically all are related to the system or device events.  

How to trigger the background task from the GUI? (I.e., when the user invokes the background task from hos or her own GUI). The answer is  ApplicationTrigger class.
 
In this article, we are going to see , how to implement this concept with simple examples.

Note This article requires the knowledge of background tasks concept. If you don’t have any idea about the background task, I highly recommend you to read the BackgroundTask complete series.

Application Trigger

This trigger is used to programmatically invoke a background task.



Enable declaration in the Package.appxmanifest in Main application.

Application Trigger is supported by the General task category. This task should be enabled in the Package.appxmainfest file.

 
 
Register Application Trigger
  1. ApplicationTrigger appTrigger;  
  2. appTrigger = new ApplicationTrigger();  
  3. var bgTask = HandlingBgTask.CreateBgTask("TaskApp""BGAppComponent.BGUIApp", appTrigger);  
Invoke-Background Trigger
 
RequestAsync function is used to trigger the background task, its function is available in the ApplicationTrigger class.

We can invoke RequestAsync function in two ways.
  1. With Arguments
  2. Without Arguments

With arguments are using the ValueSet class to pass the arguments to the background Task. Without arguments application input is not required, just call as normal function. 

Handling Trigger in the Background.
 
Once GUI has call the RequestAsync function , Background task component Run method gets invoked.
 
In this Run method , IBackgroundTaskInstance should be converted to the ApplicationTriggerDetails class, to get the values from the valueset , if it is required.
 
Best practice : One BG task component can handle mulitple bg trigger so the best way is to convert to the proper triggerdetails class 
 
 
If the ApplicationTriggerDetails contains the ValueSet, this information will be available in the Arguments property. 
  
 

Example

This below example demonstrates if the user enter the information in the text, it will show  in the toast popup message via Background Task.



Xaml Code
  1. <Grid Margin="75" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.   
  3.         <Grid.RowDefinitions>  
  4.             <RowDefinition Height="Auto"/>  
  5.             <RowDefinition Height="Auto"/>  
  6.         </Grid.RowDefinitions>  
  7.   
  8.         <Grid.ColumnDefinitions>  
  9.             <ColumnDefinition Width="Auto"/>  
  10.             <ColumnDefinition Width="Auto"/>  
  11.         </Grid.ColumnDefinitions>  
  12.   
  13.         <TextBlock Grid.Row="0" Grid.Column="0" Text="Enter the Information"/>  
  14.         <TextBox Grid.Row="0" Grid.Column="1" x:Name="txtName" PlaceholderText="Send Message"/>  
  15.         <Button x:Name="BtnTrigger" Margin="10"  
  16.                 Grid.Row="1"   
  17.                 Grid.Column="0"  
  18.                 Grid.ColumnSpan="2"  
  19.                 Content="App Trigger" HorizontalAlignment="Center"  
  20.                 Click="BtnTrigger_Click"/>  
  21.     </Grid>  
App code behind page
  1. public void CreateBGApp()  
  2.        {  
  3.            appTrigger = new ApplicationTrigger();  
  4.             
  5.            var bgTask = HandlingBgTask.CreateBgTask("TaskApp""BGAppComponent.BGUIApp", appTrigger);  
  6.   
  7.            if (bgTask != null)  
  8.            {  
  9.                bgTask.Progress += BgTask_Progress;  
  10.                bgTask.Completed += BgTask_Completed;  
  11.            }  
  12.        }
  1. private async void BtnTrigger_Click(object sender, RoutedEventArgs e)  
  2.        {  
  3.            var strName = txtName.Text;  
  4.            ValueSet value = new ValueSet();  
  5.            value.Add("UI", strName);  
  6.            var result = await appTrigger.RequestAsync(value);  
  7.        }  
BG component
  1. public sealed class BGUIApp : IBackgroundTask  
  2.     {  
  3.         BackgroundTaskDeferral bgDeferral;  
  4.         public void Run(IBackgroundTaskInstance taskInstance)  
  5.         {  
  6.             if (taskInstance == nullreturn;  
  7.             bgDeferral = taskInstance.GetDeferral();  
  8.             var appInfo = taskInstance.TriggerDetails as ApplicationTriggerDetails;  
  9.   
  10.             var txtItem = appInfo.Arguments["UI"];  
  11.             HandlingToast((string)txtItem);  
  12.             bgDeferral.Complete();  
  13.         }  
  14.   
  15.         public static void HandlingToast(string textMsg)  
  16.         {  
  17.             var BindingGeneric = new ToastBindingGeneric()  
  18.             {  
  19.                 Children =  
  20.                 {  
  21.                     new AdaptiveText()  
  22.                     {  
  23.                         Text = textMsg  
  24.                     }                      
  25.                 }  
  26.             };  
  27.             var tostVisual = new ToastVisual { BindingGeneric = BindingGeneric };  
  28.             var toastMenu = new ToastContent  
  29.             {  
  30.                 Visual = tostVisual  
  31.             };  
  32.             ToastNotification toastNotification = new ToastNotification(toastMenu.GetXml());  
  33.             ToastNotificationManager.CreateToastNotifier().Show(toastNotification);  
  34.         }  
  35.   
  36.          
  37.     }  
Hope you enjoyed and understood this.

Up Next
    Ebook Download
    View all
    Learn
    View all