使用HttpClient的进度信息

时间:2014-07-18 14:07:38

标签: c# asynchronous httpclient windows-phone-8.1

我想使用' Windows.Web.Http.HttpClient'来实现进度条。在Windows Phone 8.1 Silverlight中。

编辑:首次尝试

private async Task CheckProgress()
{
    var df = httpClient.GetAsync(new Uri(uriString, UriKind.Absolute)).Progress = Progress;

    // here i want to stop the execution till whole content downloaded

    // Reason to wait here
    // this client download will be used by one more async method. so i need to wait here till the file completely downloaded.

    // this whole is done in this method only ( requirement)
} 

private void  Progress(IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> asyncInfo, HttpProgress progressInfo)
{
   // here i getting the progress value.
   // i have already set a call back method that report the progress on the UI.
}

再尝试一次:但获得例外Invocation of delegate at wrong time.

private async Task CheckProgress()
{
    var df = httpClient.GetAsync(new Uri(uriString, UriKind.Absolute))

    df.Progress =  Progress;

    await df;
    // here i want to stop the execution till whole content downloaded
} 

问题

所有,我想要停止CheckProgress()方法等待整个下载完成。

1 个答案:

答案 0 :(得分:3)

我得到了我的问题的解决方案,但我的Second Attempt只是有点想念。

private async Task CheckProgress()
{
    var df = httpClient.GetAsync(new Uri(uriString, UriKind.Absolute))

    df.Progress = (res, progress) =>
    { 
       // no separate event handler.
    }

    await df;   
} 

它工作正常。希望它会帮助别人欢呼:)