How often is DownloadProgressChangedEventHandler called

I use Webclient to download a file asynchronously. I am wondering how often the call to DownloadProgressChangedEventHandler is called? Can the user control it?

+6
source share
2 answers

Below is the complete .NET Framework, since your question is marked as ASP.NET. (There may be different things in Silverlight.)

The short answer is: it is complex, the behavior depends on various things, including network performance characteristics, so it is incompatible, and you cannot easily control it.

Long answer:

An event is usually raised whenever the underlying thread provided by WebResponse calls the completion callback for the BeginRead operation, which WebClient uses to perform asynchronous downloads.

It seems that WebClient , as a rule, tries to read data in 64 thousand pieces. However, the streams are not required to return as much data as the requested data - it is quite possible that the BeginRead call that requests 64k will be smaller. In fact, this is quite often for streams that read data from the network - they will most likely return a smaller amount of data shortly after it appears, rather than waiting until all 64k enter.

Thus, the exact answer depends on the flow in question, and may also to some extent depend on the nature and performance of the network connection.

WebClient uses WebRequest.Create to obtain a request / response implementation that will ultimately deliver the stream, and that the extensible mechanism - .NET has 5 built-in WebRequest implementations and offers an extensibility mechanism that allows you to register additional handlers. And this is a specific implementation of WebRequest that determines the nature of the stream.

Thus, the frequency with which you receive progress events depends entirely on the type of download you are doing - you can get different results depending on what type of URL it has. (For example, http vs ftp vs file or something else.)

I'm going to take a chance that you are using HTTP.

Even then, it is quite complicated - HttpWebResponse does not always use the same thread. For example, sometimes it can return a stream received from a MemoryStream , sometimes it is a type of ConnectStream ...

Thus, you cannot say with certainty what size pieces of the main stream are likely to return, because you cannot even be sure which type of stream you are likely to receive.

As for whether you can control it, the only thing you could do is provide a custom implementation of WebRequest for a custom URL scheme. But honestly, the easiest way is to simply write code that decides whether or not to do something with a particular event, rather than trying to change the frequency of events.

+2
source share

Well, my tests show that it is pretty much vague.

The event seems to never raise less than 828 bytes and is no more than 65536 bytes.

I could not dig out any details from MSDN. Their description:

"This event occurs every time asynchronous loading has progressed. This event occurs when loading is fired using any of the following methods."

The code is used as follows:

 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 

Outputs:

  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 

10 MB change the output to:

 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 
0
source share

All Articles