0

In my UWP project l'm using in process background task. The task runs every 15 min by timer trigger in different cases:

Case1: when application is running - in this case all works fine as expect.

Case2: when application closed - nothing happened as long as application still closed.

Please help me with this problem?

Code: //Here is my BackgroundTask.Run method

var deferral = taskInstance.GetDeferral();
if (!IsApplicationClosed)
{
      //When application is running
      await Dispatcher.RunAsync(WCoreDispatcherPriority.Normal, () => {
         UpdateUI();
      });
}
else
{
    UpdateDataStorage();
}
deferral.Complete();

//Here is my OnBackgroundActivated method

base.OnBackgroundActivated(args);

var deferral = args.TaskInstance.GetDeferral();

switch (args.TaskInstance.Task.Name)
{
  case "MyBackgroundTask":
  {
      var task = new MyBackgroundTask();
      task.Run(args.TaskInstance);

  } break;
};

deferral.Complete();

Thanks.

  • There is not enough information (or code) to answer this. Have you confirmed that the OnBackgroundActivated event is indeed not fired? You could add some logging or pop up a toast from there to test it. – Stefan Wick MSFT Apr 21 '17 at 17:14
  • Have you allowed the app to run in background in your Settings? – Hannes Apr 21 '17 at 18:18
  • Have you positively confirmed that OnBackgroundActivated does not get invoked in the background case (e.g. with logging) - and that your code didn't just crash or exit prematurely? For example if your 'IsApplicationClosed' is not what you think it is, the code would crash trying to dispatch to the UI thread. Also if 'UpdateDateStorage' is an async method then you would complete the deferral and exit the process before the method has a chance to complete. – Stefan Wick MSFT Apr 22 '17 at 14:18
  • The problem is in time trigger, sometimes it was activated OnBackgroundActivated method, sometimes not. And when i'm try to debug background task from visual studio from Lifecycle Events it always work as expected in both cases. – Ihor Fastnakht Apr 24 '17 at 07:14

1 Answers1

0

I think because you're using in-process background task, the background taks and your app run on same process so when you close the application the background task will shut down, If you use out-of-process background task may solve this problem, this link help you to build out-of-process background taks: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task