question

thp avatar image
0 Votes"
thp asked AmyPeng1-MSFT edited

UWP Performance: Task.Run() vs Background Service

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

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered thp commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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.

0 Votes 0 ·