Search code examples
c#asynchronousuwpwindows-runtimewinrt-async

UWP C# : Progress of IAsyncOperationWithProgress


How do I capture the progress of an IAsyncOperationWithProgress and send it to a ProgressBar?

var packageManager = new PackageManager();
var packageUri = new Uri("https://example.com/");
var options = AddPackageByAppInstallerOptions.None;
var defaultPackageVolume = packageManager.GetDefaultPackageVolume();

var operation = packageManager.AddPackageByAppInstallerFileAsync(packageUri, options, defaultPackageVolume);

operation.Progress( ??? ); // What should I do?
MyProgressBar.Value = ???; // I'd like to display installation progress % in real time.

await operation;

Solution

  • It's easiest to call AsTask:

    var progress = new Progress<DeploymentProgress>(
        report => myProgressBar.Value = report.Percentage);
    var operation = packageManager.AddPackageByAppInstallerFileAsync(packageUri, options, defaultPackageVolume)
        .AsTask(progress);
    
    await operation;
    

    Uncompiled and untested; some manipulation re DeploymentProgress.Percentage may be necessary.