我正在使用Webclient异步下载文件。我想知道多久一次 DownloadProgressChangedEventHandler被调用?用户可以控制吗?
答案 0 :(得分:2)
以下内容适用于完整的.NET Framework,因为您的问题标记为ASP.NET。 (Silverlight可能会有所不同。)
简短回答:它很复杂,行为取决于各种因素,包括网络性能特征,所以它不一致,你不能轻易控制它。
答案很长:
每当WebResponse
提供的基础流调用BeginRead
用于执行异步下载的WebClient
操作的完成回调时,通常会引发该事件。
看起来WebClient
通常会尝试以64k块的形式读取数据。但是,流不需要返回与被叫请求的数据一样多的数据 - 请求64k的BeginRead
调用完全有可能返回更少。事实上,对于从网络读取数据的流来说,这种情况非常普遍 - 它们可能会在可用数据发布后立即返回少量数据,而不是等到所有64k数据都进入后。
所以确切的答案取决于所讨论的流,也可能在某种程度上取决于网络连接的性质和性能。
WebClient
使用WebRequest.Create
来获取最终将提供流的请求/响应实现,这是一种可扩展的机制 - .NET有5个WebRequest
的内置实现提供可扩展性机制,允许您注册其他处理程序。这是确定流的性质的特定WebRequest
实现。
因此,您获得进度事件的频率完全取决于您正在进行的下载类型 - 您可以根据它的URL类型获得不同的结果。 (例如,http vs ftp vs file,或者其他什么。)
我会冒险猜测你正在使用HTTP。
即使这样,它也非常复杂 - HttpWebResponse
并不总是使用相同类型的流。例如,它有时可以返回从MemoryStream
派生的流,有时它的类型为ConnectStream
...
所以你不能肯定地说底层流可能会返回什么大小的块,因为你甚至不能确定你可能得到什么类型的流。
至于您是否可以控制它,关于为自定义URL方案提供自定义WebRequest
实现的唯一方法。但坦率地说,编写代码决定是否对任何特定事件做任何事情而不是试图改变事件的频率可能更简单。
答案 1 :(得分:0)
我的测试似乎表明它在很大程度上是不确定的。
事件似乎永远不会少于828个字节,而且似乎不超过65536个字节。
我无法从MSDN中挖掘任何细节。他们的描述:
“此事件每次都会提出 异步下载取得进展。 下载时会引发此事件 开始使用以下任何一种方法 方法“。
代码使用如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim client As New WebClient
AddHandler client.DownloadFileCompleted, AddressOf DownloadFileCompleted
AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressCallback
Dim uri As New Uri("http://ftp.iinet.net.au/test500MB.dat", UriKind.Absolute)
client.DownloadFileAsync(uri, "test500MB.dat")
End Sub
Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Debug.Print("{0} downloaded {1} of {2} bytes. {3} % complete... Difference: {4}", _
CStr(e.UserState), e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage, CLng(e.BytesReceived - m_LastValue))
m_LastValue = e.BytesReceived
End Sub
输出:
downloaded 27416820 of 500000000 bytes. 5 % complete... Difference: 36712
downloaded 27482356 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 27483184 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 27548720 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 27550960 of 500000000 bytes. 5 % complete... Difference: 2240
downloaded 27586260 of 500000000 bytes. 5 % complete... Difference: 35300
downloaded 27651796 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 27652624 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 27718160 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 27718988 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 27755700 of 500000000 bytes. 5 % complete... Difference: 36712
downloaded 27821236 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 27822064 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 27887600 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 27888428 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 27925140 of 500000000 bytes. 5 % complete... Difference: 36712
downloaded 27990676 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 27991504 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 28057040 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 28057868 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 28094580 of 500000000 bytes. 5 % complete... Difference: 36712
downloaded 28160116 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 28160944 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 28226480 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 28227308 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 28264020 of 500000000 bytes. 5 % complete... Difference: 36712
downloaded 28329556 of 500000000 bytes. 5 % complete... Difference: 65536
downloaded 28330384 of 500000000 bytes. 5 % complete... Difference: 828
downloaded 28395920 of 500000000 bytes. 5 % complete... Difference: 65536
10mb文件输出变为:
downloaded 307533 of 10000000 bytes. 3 % complete... Difference: 28240
downloaded 369661 of 10000000 bytes. 3 % complete... Difference: 62128
downloaded 431789 of 10000000 bytes. 4 % complete... Difference: 62128
downloaded 497325 of 10000000 bytes. 4 % complete... Difference: 65536
downloaded 498153 of 10000000 bytes. 4 % complete... Difference: 828
downloaded 553221 of 10000000 bytes. 5 % complete... Difference: 55068
downloaded 618757 of 10000000 bytes. 6 % complete... Difference: 65536
downloaded 619585 of 10000000 bytes. 6 % complete... Difference: 828
downloaded 649237 of 10000000 bytes. 6 % complete... Difference: 29652
downloaded 714773 of 10000000 bytes. 7 % complete... Difference: 65536
downloaded 715601 of 10000000 bytes. 7 % complete... Difference: 828
downloaded 748077 of 10000000 bytes. 7 % complete... Difference: 32476
downloaded 813613 of 10000000 bytes. 8 % complete... Difference: 65536
downloaded 814441 of 10000000 bytes. 8 % complete... Difference: 828
downloaded 866685 of 10000000 bytes. 8 % complete... Difference: 52244
downloaded 932221 of 10000000 bytes. 9 % complete... Difference: 65536
downloaded 933049 of 10000000 bytes. 9 % complete... Difference: 828
downloaded 982469 of 10000000 bytes. 9 % complete... Difference: 49420
downloaded 1048005 of 10000000 bytes. 10 % complete... Difference: 65536
downloaded 1048833 of 10000000 bytes. 10 % complete... Difference: 828
downloaded 1114369 of 10000000 bytes. 11 % complete... Difference: 65536
downloaded 1115197 of 10000000 bytes. 11 % complete... Difference: 828