c# - 使用 HttpClient 的进度信息

标签 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.
}

再试一次:但是得到异常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() 方法继续等待,直到整个下载完成。

最佳答案

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

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

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

    await df;   
} 

一切正常。

关于c# - 使用 HttpClient 的进度信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827009/

相关文章:

c# - 如何让我的通用比较器 (IComparer) 处理空值?

c# - EntityFramework 和带有对象数组的自定义类(一对多)

c# - 适用于 Windows 8 的简单数据库

asynchronous - 为什么在 crossbeam_channel::select 旁边调用时 tokio::spawn 有延迟?

c# - 新Unity Input系统1.0.0下如何处理多键输入

spring - 异步微服务通信如何从UI的 Angular 表现得更好?

asynchronous - 在 Julia 中使用 @sync @async 的简单并行性

ruby - 有没有办法在 Ruby 中使用 HTTPClient 获取错误响应?

xamarin.ios - Xamarin.iOS项目上带有HttpClient的“Operation is not valid”错误

java - 使用 Apache HttpClient 3.0.1 的间歇性不完整响应