UWP Performance: Task.Run() vs Background Service

Thomas Pentenrieder 36 Reputation points โ€ข MVP

I'm working on a UWP app that is showing videos, images & XAML animations. The content displayed is being updated from time to time by pushing updates from the server to the app through grpc.

When updates happen the app will need to download new assets like potentially big video files. While this happens it is important that the UI does not get any negative impact and the fps remain stable.

From what I've read so far it can help to wrap the download operation inside a Task.Run() instead simply awaiting the HttpClient call to not affect the UI thread.

Would there be any additional performance improvements if I'd move the download operation to a background service altogether? Or would this be unnecessary overhead? (I don't need downloads to work while the app is not running.)

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Fay Wang - MSFT 5,231 Reputation points

Hello,
โ€‹

Welcome to our Microsoft Q&A platform!

>>Would there be any additional performance improvements if I'd move the download operation to a background service altogether? Or would this be unnecessary overhead?

Using Task.Run and Background task to download new assets, both of them won't affect the UI thread. But if you want to download large files, it's better to use background transfer API. Background Transfer runs separately from the calling app and is primarily designed for long-term transfer operations for resources like video, music, and large images and Httpclient needs to occupy the app's resources. For more information about how to use background transfer, you can refer to this document and here is an official sample, you can check it.

>>I don't need downloads to work while the app is not running.

You can pause your transfer before your app exits. For example, when you prepare to exit the app, you can use the following code in Suspending event to pause all download operations.

private void App_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e) 
{ 
 foreach (DownloadOperation download in activeDownloads) 
 {โ€‹ 
 BackgroundDownloadProgress currentProgress = download.Progress;โ€‹ 
 if (currentProgress.Status == BackgroundTransferStatus.Running)โ€‹ 
 {โ€‹ 
 download.Pause();โ€‹ 
 
 }โ€‹elseโ€‹ 
 {โ€‹ 
 ...... 
 }โ€‹ 
 } 
} 
  1. Thomas Pentenrieder 36 Reputation points โ€ข MVP

    Great hint, thanks!
    I don't really need to suspend the the downloads, it's just not a requirement that they continue once the app closes. But it doesn't hurt if they do.

1 additional answer

  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more