使用 HttpClient 的进度信息

loo*_*oop 4 c# asynchronous httpclient windows-phone-8.1

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

编辑:第一次尝试:

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.
}
Run Code Online (Sandbox Code Playgroud)

再一次尝试:但出现异常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
} 
Run Code Online (Sandbox Code Playgroud)

问题:

我想要的只是停止该CheckProgress()方法以继续等待整个下载完成。

loo*_*oop 5

我得到了问题的解决方案,但在我的第二次尝试中只是有点失误:

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

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

    await df;   
} 
Run Code Online (Sandbox Code Playgroud)

它工作得很好。