More Application Optimizing for 256 MB Devices

Background Tasks

Make sure you have exception handling in your Background Task scheduling code. This prevent your app from crashing when run on 256MB devices, if the maximum number of tasks have been scheduled and allowed and if Background Tasks have been disabled by the user. For example:

try
{
    ScheduledActionService.Add(periodicTask);
}
catch (InvalidOperationException exception)
{
    if (exception.Message.Contains("BNS Error: The action is disabled"))
    {
        MessageBox.Show("Background agents for this application have been disabled by the user.");
    }
    if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added."))
    {
        // No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.
    }
}
catch (SchedulerServiceException)
{
    // No user action required.
    PeriodicCheckBox.IsChecked = false;
}

The ability to run code truly in the background is a great feature of Windows Phone 7.1. However, your apps should not be dependent on background tasks. Why? For three reasons:

  • PeriodicTasks can be disabled on an app by app basis. Check out Settings | applications | background tasks on your phone. You will see the list of applications that are using background tasks. Here the user can disable your applications use of background tasks.
  • There is a maximum number of PeriodicTask tasks that can be run. Once this limit is hit, no further tasks can be scheduled.
  • PeriodicTask and ResourceIntensiveTask are not supported on 256MB devices. However, you can include code for these in your application – just understand that these tasks will not run on this class of device. This makes it easy to write a single application that will run on all devices types. In fact on these devices, you will see the ‘background tasks’ is still part of the Settings menu (Settings | applications | background tasks).

If your app is running on a 256 device and includes background tasks, you will see that they are disabled in the applications settings (Settings | applications | background tasks | advanced). See in the screen shot (running the 256MB emulator) below, the background tasks included in the application ‘BackgroundAgentSample’ are disabled.

image

When the maximum number of PeriodicTasks have been scheduled, and you attempt to add another, a InvalidOperationException exception is thrown. The exception message is ‘BNS Error: The maximum number of ScheduledActions of this type have already been added.’ On 256MB devices, this is called anytime you attend to add a scheduled task since the maximum number of PeriodicTasks allowed on these devices is zero.

Note: On premium devices, if you attempt to call ‘Add’ and background agents have been disabled, the exception InvalidOperationException is thrown.

Full SDK sample here.

 

 

 

 

Additional Best Practices

Optimizing Apps for Lower Cost Devices
Developing for 256-MB Devices
Best practice tips for delivering apps to Windows Phone with 256 MB – Nokia Developer Wiki

Leave a Reply

Your email address will not be published. Required fields are marked *