Iris Classon
Iris Classon - In Love with Code

Stupid Question 144: When are background tasks terminated in WinRT and do you get notified?

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

When are background tasks terminated in WinRT and do you get notified?

This was another question I got at the BISY user group after the WinRT session. My answer was that they get terminated under the same conditions as an app gets terminated (when the system is low on resources) – but as for the notification I assumed that you would be notified, but I wasn’t sure of the implementation or the exact details. I’ve handled the cancellation event, but I asked to get back to the person that asked so I could double check.

As a quick intro I can mention that background tasks are tasks that run in the background (yes I know- brilliant explanation so far) and you can use them from quite a few cool things. You can push live tile notifications, use them for real time communication and more- this while your app is suspended or not running.

All background tasks should register for progress, completed and cancellation and should be lightweight. For the specifics have a read here. Notice the difference for the device is running on battery (DC power) for the network quota. As for CPU usage limitations:

The cancellation handler looks like this:

void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason){}

Notice that you can get the reason for the cancellation.

Once a backgroundtask is cancelled you have 5 second to save data or do whatever you need to do.

Here is a great whitepaper on Background tasks with all the do’s and don’ts

Comments

Leave a comment below, or by email.
Johan Sundström
2/12/2013 4:07:11 AM
OMG, if you call this a stupid question I cannot even imagine what level eventual "smart" questions would be. 

Just keep up the good work! 
Tom McKearney
1/31/2014 8:36:15 AM
Is there any way to find out *why* an app was terminated?

My app, when in the foreground and idle just disappears from the screen.  I have the Diagnostic log enabled in the event log.  I only notice that my background task runs (it's almost a NOOP, barely any time or CPU or Network usage) and "returned with error code 0x0", which sseems fine.

Then, right after that, the application "has changed to state terminated".

Well, what happened?  I'm at a total loss. 


Last modified on 2013-02-06

comments powered by Disqus