PrepAway - Latest Free Exam Questions & Answers

Which four lines of code should you use to create the Run() method on line SP13?

DRAG DROP
You need to call the DoWork() method asynchronously in a background task.
Which four lines of code should you use to create the Run() method on line SP13? (To answer, move
the appropriate lines of code from the list of code segments to the answer area and arrange them in
the correct order.)

PrepAway - Latest Free Exam Questions & Answers

Answer: See the explanation.

Explanation:

Box 1:

Box 2:

Box 3:

Box 4:

Note:
* IBackgroundTaskInstance.GetDeferral method
Informs the system that the background task might continue to perform work after the
IBackgroundTask.Run method returns.
* Example:
public async void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine(“Start background download in background task!!!”);
// Associate a cancellation handler with the background task.
//taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
// Get the deferral object from the task instance, and take a reference to the taskInstance;
_deferral = taskInstance.GetDeferral();

//m_BKDownload.ResumeAll();
// Check if file already downloaded
string inputuri = “http://www.johnhaydon.com/wpcontent/uploads/2011/07/google_plus_logo.jpg”;
string FileName = inputuri.Split(new[] { ‘/’ }).LastOrDefault();
bool b_fileFounded = false;
IReadOnlyList<StorageFile> outputFiles = await KnownFolders.PicturesLibrary.GetFilesAsync();
foreach (var file in outputFiles)
{
if (file.Name == FileName)
{
b_fileFounded = true;
}
}
if (b_fileFounded == false)
{
Debug.WriteLine(“downloading {0} in the background task”, FileName);
StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
FileName, CreationCollisionOption.GenerateUniqueName);
var downloadOperation = m_BKDownload.CreateDownload(new Uri(inputuri),
destinationFile);
await downloadOperation.StartAsync().AsTask();
}
_deferral.Complete();
}


Leave a Reply