多久DownloadProgressChangedEventHandler被称为 [英] How often will DownloadProgressChangedEventHandler be called

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

问题描述

我使用的Web客户端下载文件异步。我想知道是多久 DownloadProgressChangedEventHandler被调用?可以在用户控制它?

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

推荐答案

以下内容适用于完整的.NET框架,因为你的问题是标签的ASP.NET。 (事情可能会在Silverlight不同。)

The following applies to the full .NET Framework, since your question is tagged ASP.NET. (Things might be different in Silverlight.)

简短的回答:这是复杂的,行为取决于很多方面,包括网络性能,所以它的不一致,你不能轻易地控制它

Short answer: it's complex, the behaviour depends on various things including network performance characteristics, so it's inconsistent and you can't easily control it.

的回答:

本次活动将通常每次提出的 WebResponse类调用了的BeginRead 的操作, Web客户端用来执行异步下载。

The event will typically be raised each time the underlying stream provided by the WebResponse invokes the completion callback for the BeginRead operation that WebClient uses to perform asynchronous downloads.

看起来 Web客户端通常会试图在64K块读取数据。然而,流不要求作为被叫要求返回尽可能多的数据 - 这是完全有可能请求64K调用的BeginRead 将返回更少。事实上,那是从网络读取数据流相当普遍 - 他们很可能会返回一个较小的数据量是后不久即可提供,而不是等到所有64K进来了。

It looks like WebClient will typically attempt to read data in 64k chunks. However, streams are not required to return as much data as the called asked for - it's entirely possible that a call to BeginRead that requests 64k will return less. In fact, that's quite common for streams that read data from the network - they are likely to return a smaller amount of data shortly after it's available, rather than waiting until all 64k has come in.

因此​​,确切的答案取决于所讨论的流,并且还可以上取决于网络连接的性质和性能在一定程度上

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

Web客户端使用 WebRequest.Create 获取请求/响应实现,这将最终提供的数据流,这是可扩展的机制 - .NET有5个内置的的WebRequest 的实现,它提供了一个扩展机制,允许您注册更多的处理程序。而它的具体的WebRequest 的实施决定了流的性质。

WebClient uses WebRequest.Create to obtain the request/response implementation that will ultimately supply the stream, and that's an extensible mechanism - .NET has 5 built-in implementations of WebRequest and it offers an extensibility mechanism that lets you register additional handlers. And it's the specific WebRequest implementation that determines the nature of the stream.

所以,与您取得进步事件的频率完全取决于下载你正在做的类型 - 你可以得到不同的结果取决于它是什么样的URL。 (例如,HTTP VS FTP VS文件,或什么的。)

So the frequency with which you get progress events is entirely up to the type of download you're doing - you can get different results depending on what sort of URL it is. (E.g., http vs ftp vs file, or whatever.)

我要大胆地胡乱猜测,你正在使用HTTP。

I'm going to hazard a wild guess that you're using HTTP.

即使是这样,这是pretty的复杂 - 在 HttpWebResponse 并不总是使用同一种流。例如,它有时会返回从的MemoryStream 派生的流,有时它是类型 ConnectStream ...

Even then, it's pretty complicated - the HttpWebResponse doesn't always use the same kind of stream. For example, it can sometimes return a stream derived from MemoryStream, sometimes it's of type ConnectStream...

所以,你不能说有把握什么样大小的块底层流很可能会回来,因为你甚至不能确定是什么类型的流,你很可能得到的。

So you can't say with any certainty what size chunks the underlying stream is likely to return because you can't even be sure what type of stream you're likely to get.

至于是否可​​以控制它,你可能会提供一个自定义的的WebRequest 实现自定义URL方案的唯一途径。但坦率地说,它可能只是简单写code,它决定是否做任何特定事件东西,而不是试图改变事件的频率。

As for whether you can control it, about the only way you could would be to provide a custom WebRequest implementation for a custom URL scheme. But frankly, it's probably simpler just to write code that decides whether or not to do anything with any particular event rather than trying to change the frequency of the events.

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

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