Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement IAsyncOperationWithProgress

I'm porting some custom .NET streams to WINRT. Language is C#.

Is there some example implementation of IAsyncOperationWithProgress? Since Methods ReadAsync, WriteAsync from Windows.Storage.Streams require them. Custom WinRT streams implementations are welcomed also.

I found some C examples using create_async, but i'm looking to do this in C#, and i cannot find create_async in the Metro framework.

Thanks in advance

like image 871
Maximo Piva Avatar asked Apr 11 '12 19:04

Maximo Piva


1 Answers

Here is an example of using IAsyncOperationWithProgress to display the progress of installing an XAP file programatically. I'm pretty new to Win8 development so not sure if it's entirely idiomatic.

Note the Dispatcher.BeginInvoke to marshall the progress back to the UI thread. Hope it helps:

private async void InstallApp(string name, Uri uri)
{
    try
    {
        StatusTextBlock.Text = "Installing app";
        var installTask = InstallationManager.AddPackageAsync(name, uri);

        installTask.Progress = (installResult, progress) => Dispatcher.BeginInvoke(() =>
        {
            StatusTextBlock.Text = "Progress: " + progress;
        });

        var result = await installTask;
        StatusTextBlock.Text = "Done: " + result.InstallState.ToString();
    }
    catch (Exception ex)
    {
        StatusTextBlock.Text = "Failed to install: " + ex.Message;
    }
}
like image 158
Lee Richardson Avatar answered Oct 20 '22 21:10

Lee Richardson