WebClient DownloadProgressChangedEventHandler 未触发 [英] WebClient DownloadProgressChangedEventHandler Not Firing

查看:28
本文介绍了WebClient DownloadProgressChangedEventHandler 未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法触发 DownloadProgressChangedEventHandler.我知道,在最坏的情况下,事件处理程序 应该每 64Kb 触发一次.我尝试从中下载数据的 URL 会动态创建 680Kb 的 XML 数据,但处理程序根本不触发.

I'm having trouble getting DownloadProgressChangedEventHandler to fire. I understand that, worst case, the event handler should fire every 64Kb. The URL I'm trying to download data from creates 680Kb of XML data on the fly, but the handler does not fire at all.

这是演示问题的测试代码.很遗憾,我无法分享特定 URL,因为它包含专有数据.

Here's test code that demonstrates the problem. Unfortunately I cannot share the specific URL as it contains proprietary data.

static void Main(string[] args)
{
    Console.WriteLine("Downloading data");
    string url = "https://MyUrlThatRespondsWith680KbOfData";
    string outputPath = @"C:\Temp\DeleteMe.xml";
    using (WebClient webClient = new WebClient())
    {
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
        webClient.DownloadFile(url, outputPath);
    }
    Console.Write("Done");
    Console.ReadKey(true);
}

static void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    Console.WriteLine("Download progress: " + e.BytesReceived);
}

推荐答案

您的代码看起来不错,但 documentation 说每次异步下载取得进展时都会引发此事件",而您正在使用下载的同步版本.切换到使用 DownloadFileAsync.

Your code looks ok, but documentation says "This event is raised each time an asynchronous download makes progress" while you are using synchronous version of the download. Switch to use DownloadFileAsync.

这篇关于WebClient DownloadProgressChangedEventHandler 未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆