UWP Performance: Task.Run() vs Background Service

Thomas Pentenrieder 36 Reputation points MVP
2019-11-04T11:14:01.42+00:00

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

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,191 Reputation points
    2019-11-05T05:37:35.987+00:00

    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​  
            {​  
                ......         
            }​  
        }  
    }  
    

0 additional answers

Sort by: Most helpful