Background Task In Universal Windows Program - Part Four

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

Introduction

In Windows 10, the background task is hosted into a separate process. That means, the main application runs in different processes, while the background task runs in the separate process.

Windows 10 Anniversary Update (Version 1607 ) includes one more feature in the background process. Background process is now  included into the application process, and this single process handles them both, the application(GUI) and the background task.

Application life cycle

Application life cycle process has had several major changes to implement the single process concept.

 Application object.

life cycle
Source Image: MSDN

Initially, the application is not running. App object enters the Background Task activity and Background Task has stopped when application is running. To implement this concept, Application object has two events to be implemented:
  1. Application. Current. EnteredBackground
  2. Application. Current. LeavingBackground

These events play a major role in handling the Background task (In older versions of the Windows, Runtime Component handles all this stuff).

Sample Code

  1. Application.Current.EnteredBackground += app_EnteredBackground;   
  2. Application.Current.LeavingBackground += app_LeavingBackground;  
Application. Current. EnteredBackground

Application is into the non-running mode. This event gets triggered and the background task starts here. The application process enters the background activities and application reaches the suspended state forever. This is the best place to store the application session state.
  1. private void app_LeavingBackground(object sender, LeavingBackgroundEventArgs e)   
  2. {   
  3.   
  4. }  
Ex: Application is minimized on desktop.


EnteredBackground

Application. Current. LeavingBackground

Application enters the Visible mode. This event gets triggered, application process exits into the background activities and handles the UI based functionality, this is best place to handle the activated application  or resume events functionality here.
  1. private void app_EnteredBackground(object sender, EnteredBackgroundEventArgs e) {   
  2.   
  3. }   
Ex: Application is maximizing on  the desktop.

LeavingBackground

Register Background Task

Register background task into the single process by following the same steps as the normal background task steps. The only difference in both the processes is the TaskEntryPoint property. In normal Background Tasks, the TaskEntryPoint property is used to identify the BackgroundTask class name while in the current process, it is not required because the background task is running in its own application process object.

Sample Code
  1. private static IBackgroundTaskRegistration RegisterBgTask(string bgTaskName)   
  2. {  
  3.         if (!IsAppHasPermission().Result) return null;  
  4.         var bgRegister = IsBackgroundRegister(bgTaskName);  
  5.         if (bgRegister != nullreturn bgRegister;  
  6.         var objBg = new BackgroundTaskBuilder({  
  7.                 Name = bgTaskName  
  8.             }; objBg.SetTrigger(_bgTrigger);  
  9.             if (_bgCondition != null) objBg.AddCondition(_bgCondition); bgRegister = objBg.Register();  
  10.             return bgRegister;  
  11.         }  
Background Run Method

Based on the trigger registration, BackgroundTask component Run method gets invoked.

public void Run (IBackgroundTaskInstance task Instance)
- This method is handling the performance of the BackgroundTask in Windows Runtime component. In Single process BackgroundTask, this run method is handled by the OnBackgroundActivated function, in the Application class.
  1. protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) {  
  2.     base.OnBackgroundActivated(args);  
  3. }  
Sample Code

Background Task in Universal Windows Program - Part Two sample Windows Runtime component code has moved into our main application RunBackGroundTask function. The code will look like the following:
  1. private async void RunBackGroundTask(IBackgroundActivatedEventArgs args)   
  2. {  
  3.         var bgInstance = args.TaskInstance;  
  4.         if (bgInstance == nullreturn;  
  5.         _appServiceDeferral = bgInstance.GetDeferral();  
  6.         bgInstance.Canceled += BgInstance_Canceled;  
  7.         await ToastNotificationUpdate("Time Zone has changed");  
  8.         _appServiceDeferral.Complete();  
  9.     }  
  10.     //And OnBackgroundActivated call the RunBackGroundTask to handle the BG task.   
  11. protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) {  
  12.     base.OnBackgroundActivated(args);  
  13.     RunBackGroundTask(args);  
  14. }  
output

In the upcoming article, we will see more on this topic.

 


Similar Articles