Background Task in Universal Windows Program - Part Two

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

In this article we will see through code how to implement the Background Task.

Background Task: Windows Runtime Component
  1. File -> Windows -> Universal -> Windows Runtime Component (Universal Windows),

    wrc

Add interface IBackgroundTask and implement the Run method and Canceled event.

code

Main Application

Create New Project -> Blank App (Universal Windows) (I have used the same solution.)

Package.appxmanifest

Open the Package.appxmanifest -> Declarations-> Available Declarations -> Select & Add-> Background Tasks.

Select which background task should support your app (for this example I used the System event).

background task

And App settings -> Add the Entry point -> BGProcess.BgTask (Windows Runtime Component Namespace.classname),

Add

Add Windows Runtime class as a Reference

Reference

Implement Background Task into Main Project

Before creating Background task first check whether BackgroundExecutionManager app has required permission or not.

code

Check whether Background is registered or not.

If App has permission check if task is registered already or not.

BackgroundTaskRegistration class, check whether task already is registered or not. If register returns the BackgroundTaskObject, Check with Background Task Name,

code

If not registered create BackgroundTaskBuilder & register into the BackgroundTaskRegistration class.

Create BackgroundTaskBuilder Object

To create BackgroundTask object Name and TaskEntryPoint must be specified.

Task Name (Name of the BG task name) and Task Entry Name: Windows Runtime component (Namespace with Class Name),

code

Define the Trigger when the background task should run and add the condition to raise the event (this is optional),

code

And register into the BackgroundTaskRegistration Manager,
BackgroundTaskRegistration
Complete function
code

Implement callback to receive the input from Background to trigger the event

UI Design to test BG Task.

Xaml coding

code

Same Code behind page

  1. private void BtnBgStart_OnClick(object sender, RoutedEventArgs e)   
  2. {  
  3.     BackgroundTask = HandlingBgTask.CreateBgTask(_bgTaskName, "BGProcess.BgTask");  
  4.     if (BackgroundTask == nullreturn;  
  5.     BackgroundTask.Completed += _backgroundTask_Completed;  
  6.     BackgroundTask.Progress += _backgroundTask_Progress;  
  7.     ActiveStatus = true;  
  8.     TxtStatus.Text = "Name : " + BackgroundTask.Name;  
  9.   
  10.     StatusUpdate = "Waiting for Background event";  
  11. }  
  12. private async void _backgroundTask_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args) {  
  13.     await Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>  
  14.     {  
  15.         StatusUpdate = "Get Alert from BG Process :" + sender.Name;  
  16.         ActiveStatus = false;  
  17.     });  
Go to settings -> Time & language -> Date & Time and change time zone background task to trigger the changed event, 
  1. Before Change, 

    Before

  2. After change, 

    after
Note: When time zone events are triggered only the time is different.

Up Next
    Ebook Download
    View all
    Learn
    View all