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.
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:
- Application. Current. EnteredBackground
- 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
- Application.Current.EnteredBackground += app_EnteredBackground;
- 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.
- private void app_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
- {
-
- }
Ex: Application is minimized on desktop.
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.
- private void app_EnteredBackground(object sender, EnteredBackgroundEventArgs e) {
-
- }
Ex: Application is maximizing on the desktop.
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
- private static IBackgroundTaskRegistration RegisterBgTask(string bgTaskName)
- {
- if (!IsAppHasPermission().Result) return null;
- var bgRegister = IsBackgroundRegister(bgTaskName);
- if (bgRegister != null) return bgRegister;
- var objBg = new BackgroundTaskBuilder({
- Name = bgTaskName
- }; objBg.SetTrigger(_bgTrigger);
- if (_bgCondition != null) objBg.AddCondition(_bgCondition); bgRegister = objBg.Register();
- return bgRegister;
- }
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.
- protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) {
- base.OnBackgroundActivated(args);
- }
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:
- private async void RunBackGroundTask(IBackgroundActivatedEventArgs args)
- {
- var bgInstance = args.TaskInstance;
- if (bgInstance == null) return;
- _appServiceDeferral = bgInstance.GetDeferral();
- bgInstance.Canceled += BgInstance_Canceled;
- await ToastNotificationUpdate("Time Zone has changed");
- _appServiceDeferral.Complete();
- }
-
- protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) {
- base.OnBackgroundActivated(args);
- RunBackGroundTask(args);
- }
In the upcoming article, we will see more on this topic.