Feb 062013
 February 6, 2013  Posted by at 9:57 am Not So Stupid Questions  Add comments

[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?

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:

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

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

  2 Responses to “Stupid Question 144: When are background tasks terminated in WinRT and do you get notified?”

  1. 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!

  2. 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.

Leave a Reply to Tom McKearney Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



What is 12 + 3 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)