How to implement IAsyncOperationWithProgress

血红的双手。 提交于 2019-12-07 03:40:02

问题


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


回答1:


For C#, you should check out this //build talk on async programming.

If you're using high level C++, you should look at this article about how to do asynchronous operations in the PPL.

If you need to implement your own async operation from low level C++, you should look at the WRL::AsyncBase class.




回答2:


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


来源:https://stackoverflow.com/questions/10112696/how-to-implement-iasyncoperationwithprogress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!