UWP C# : Progress of IAsyncOperationWithProgress

765 Views Asked by At

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;
1

There are 1 best solutions below

0
Stephen Cleary On BEST ANSWER

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.