How to Add Support for Background Transfers in your Windows Phone Application

Introduction

Today, mobile applications hardly persist data on the
devices. They get their information from the cloud. A typical example is an
application that streams YouTube videos or downloads data to work on.

Whenever there is a large file to work on, the user
experience can be dismal if the application developer does a synchronous
download of the file. The asynchronous file transfer can be a workable solution
as long as the application can let the user know that a download has been
queued and notify the user when it is complete.

Windows
Phone "Mango" introduced support for background transfers. Let us write an
application that makes use of the "Background transfer" feature in a
Windows Phone application.

Hands-On

Add a Button and two TextBox controls to the MainPage.xaml.
Rename the first TextBox control to "textBoxStatus", the second TextBox to
"textBoxProgress" and the Button control to "buttonAddToTransfer".

Add an Event for Click on the button buttonAddToTransfer. To
do this, double click the buttonAddToTransfer control on MainPage.xaml.

Compile and run your application. When we run the
application for the first time, we see the screen shown below.

Figure 1: Run the application for the first time

Compile and run your application

Click the "Button" control to add the background transfer
request to the queue.

Figure 2: Click the "Button" control to add the background transfer
request to the queue.

Add the background transfer request to the queue

You will notice that the transfer starts and the Status and
Progress textboxes get updated with the current status, which is "Transferring"
and the current progress.

When the transfer is complete, you will notice the Status to
be completed and Progress to be 100.

This implies that the background transfer has completed
successfully.

Figure 3: The background transfer has completed

This was a very simple example of using the
BackgroundTransferRequest class. In a future article, we will visit the best
practices of using the BackgroundTransferRequest class.

If you are having trouble following along, you can download
the sample code below.

Summary

In this article, we learned about using the
BackgroundTransferRequest class to add an asynchronous download request in a
Windows Phone "Mango" application. I hope you have found this information
useful.

About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com