0

BackgroundTaskDeferral.Complete works normally when the application runs with debugging, the main project can catch OnComplete event even when the app is suspended.

But when the app runs without debugging and the app goes to background, BackgroundTaskDeferral.Complete does not work. the main project can not receive anything when background task call (when main project catch event OnComplete I will show a toast notification)

BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
_deferral.Complete();
ThangBM
  • 301
  • 3
  • 10

1 Answers1

1

If I understood you correct, the problem is that your main project doesn't get notified one Background Task completes its work. It's normal - your main project is being suspended by the OS, shortly after your app goes to background.

It works while you are debugging, hence the PLM is disabled and suspending/resuming events are not being raised.

If you want to perform some job in the background - put it inside background task, sending toast notification should work fine.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • So background task can not call back to function in main project when application is closed,Does BackgroundtaskDeferal.Complete just work when application in foreground ? – ThangBM Jun 15 '16 at 08:46
  • @ThangBM Deferal.Complete is used to work with async methods in background task. If you have an async method run on other thread, then without deferal the Run method will get to it's end and the OS will close your task. When you have obtained a deferal, the system knows that something else is going on, when you complete it then you give a sign to OS that it can free the resources. – Romasz Jun 15 '16 at 10:20
  • Thank you so much, so, in case, app is closing,when it receives notification trigger at background task, I want to query the database at main project, what should I do ? – ThangBM Jun 16 '16 at 04:43
  • @ThangBM Do what you want with your database in background task, then once main project is launched/resumed, reload the database. – Romasz Jun 16 '16 at 05:25
  • @Romaz : I want to query database and display some data on toast notification when application receives notification. but maybe I have to query database at background task – ThangBM Jun 16 '16 at 07:08
  • @ThangBM That's what I'm talking about - give it a try. – Romasz Jun 16 '16 at 07:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114812/discussion-between-thangbm-and-romasz). – ThangBM Jun 16 '16 at 08:04
  • @Romaz: I have another question relate to background task, if you can, please help me to answer it, thanks . stackoverflow.com/questions/38066145/can-not-call-task-run-at-backgroundtask – ThangBM Jun 28 '16 at 02:38