问题描述
据
的,
OpenFileAsync应该DownloadProgressChanged射击时,它使人进步。
OpenFileAsync should have DownloadProgressChanged firing whenever it makes progress.
我不能让它火的。火灾罚款DownloadDataAsync和DownloadFileAsync反而虽然。
I can't get it to fire at all. Fires fine with DownloadDataAsync and DownloadFileAsync instead though.
下面是一个简单的例子:
Here's a simple example:
using System;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadAsync(new Uri("http://www.stackoverflow.com"));
Console.ReadKey();
}
static void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("{0}% Downloaded", e.ProgressPercentage);
}
static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
Console.WriteLine("Open Read Completed");
}
}
}
有关我的DownloadProgressChanged事件永远不会触发,但变化DownloadFileAsync或DownloadDataAsync和它的作用。
For me the DownloadProgressChanged event never fires, although change to DownloadFileAsync or DownloadDataAsync and it does.
推荐答案
我看过的框架来源,据我可以告诉OpenReadAsync从来没有触及触发DownloadProgressChanged的东西。
I've looked at the framework source and as far as I can tell the OpenReadAsync never touches the stuff that triggers DownloadProgressChanged.
这不叫喜欢DownloadDataAsync和DownloadFileAsync做,而这又似乎什么被拉开活动的GetBytes。
It doesn't call GetBytes like DownloadDataAsync and DownloadFileAsync do, which is what in turn appears to be kicking off the event.
要解决它,我只是用DownloadDataAsync代替,这确实触发事件,并让我提供下载的UI反馈。它返回一个字节数组,而不是我所需要的数据流,但是这不是一个问题。
To work around it I've just used DownloadDataAsync instead, which does trigger the event and allows me to provide UI feedback for the download. It returns a byte array instead of the stream I needed, but that's not an issue.
所以我假定这是MSDN那是错在这里,和OpenReadAsync不会触发DownloadProgressChanged。
So I'm assuming that it's MSDN that's wrong here, and OpenReadAsync doesn't trigger DownloadProgressChanged.
这篇关于是否WebClient.OpenFileAsync火DownloadProgressChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!