WebClient.OpenFileAsync是否触发DownloadProgressChanged

根据

http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadprogresschanged.aspx ,

OpenFileAsync应该在每次进行时触发DownloadProgressChanged。

我根本无法解雇它。 不过,可以使用DownloadDataAsync和DownloadFileAsync来解决问题。

这是一个简单的例子:

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,但它确实如此。

我查看了框架源代码,据我所知,OpenReadAsync从不触及触发DownloadProgressChanged的东西。

它没有像DownloadDataAsync和DownloadFileAsync那样调用GetBytes,这反过来似乎开始了这个事件。

为了解决这个问题,我刚刚使用了DownloadDataAsync,它确实触发了事件并允许我为下载提供UI反馈。 它返回一个字节数组而不是我需要的流,但这不是问题。

所以我假设它的MSDN在这里是错误的,OpenReadAsync不会触发DownloadProgressChanged。