title: How to run in-process background task when the app is killed in UWP?
body: <p>I want my in-process background task to run when the app is killed. This is to constantly track the user's location changes. Not geofencing. So, is there a way to do this in UWP? Following is my code and it gets killed when the app is killed. It took only a few minutes to do this on Android (Sticky background service) &amp; iOS. Using a WindowsRuntimeComponent with out of process task is not possible because I have a lot of code to refer from my Xamarin Forms PCL. <strong>The equivalent of what I'm looking for is Android Sticky Background Service.</strong></p>
  
  <pre><code>private async void RegisterBackgroundTask() 
  {
      var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
  
      if (backgroundAccessStatus == BackgroundAccessStatus.AllowedSubjectToSystemPolicy)
      {
          UnregisterTask(TaskName);
          RegisterTask(TaskName);
      }
  }
  
  private async void RegisterTask(string taskName)
  {
      BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder
      {
          Name = taskName,
      };
  
      var trigger = new ApplicationTrigger();
      taskBuilder.SetTrigger(trigger);
  
      var registration = taskBuilder.Register();
      await trigger.RequestAsync();
  }
  
  
  protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
  {
      Debug.WriteLine("Background " + args.TaskInstance.Task.Name + " Starting...");
      SendToast("Hi this is background Task");
      //
      // Query BackgroundWorkCost
      // Guidance: If BackgroundWorkCost is high, then perform only the minimum amount
      // of work in the background task and return immediately.
      //
      var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;
      var settings = ApplicationData.Current.LocalSettings;
      settings.Values["BackgroundWorkCost"] = cost.ToString();
  
      //
      // Associate a cancellation handler with the background task.
      //
      args.TaskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
  
      //
      // Get the deferral object from the task instance, and take a reference to the taskInstance;
      //
      _deferral = args.TaskInstance.GetDeferral();
      _taskInstance = args.TaskInstance;
  
      _periodicTimer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(PeriodicTimerCallback), TimeSpan.FromSeconds(1));
  }
  
  private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
  {
      //
      // Indicate that the background task is canceled.
      //
      _cancelRequested = true;
      _cancelReason = reason;
  
      SendToast("Background " + sender.Task.Name + " Cancel Requested...");
  }
  
  private void PeriodicTimerCallback(ThreadPoolTimer timer)
  {
      if ((_cancelRequested == false) &amp;&amp; (_progress &lt; 100))
      {
          _progress += 10;
          _taskInstance.Progress = _progress;
          SendToast("Timer Went Off!!");
      }
      else
      {
          _periodicTimer.Cancel();
  
          var key = _taskInstance.Task.Name;
  
          //
          // Record that this background task ran.
          //
          String taskStatus = (_progress &lt; 100) ? "Canceled with reason: " + _cancelReason.ToString() : "Completed";
          var settings = ApplicationData.Current.LocalSettings;
          settings.Values[key] = taskStatus;
          Debug.WriteLine("Background " + _taskInstance.Task.Name + settings.Values[key]);
          SendToast(taskStatus);
          //
          // Indicate that the background task has completed.
          //
          _deferral.Complete();
      }
  
  }
  
  public static void SendToast(string message)
  {
      var template = ToastTemplateType.ToastText01;
      var xml = ToastNotificationManager.GetTemplateContent(template);
      var elements = xml.GetElementsByTagName("text");
      var text = xml.CreateTextNode(message);
  
      elements[0].AppendChild(text);
      var toast = new ToastNotification(xml);
      ToastNotificationManager.CreateToastNotifier().Show(toast);
  }
  </code></pre>