How to implement IAsyncOperationWithProgress

痴心易碎 提交于 2019-12-05 07:18:30

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.

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