简体   繁体   English

使用C#的进度和速度下载/上传最有效/准确的方式?

[英]Most efficient/accurate way to download/upload with progress and speed in C#?

Uploading and downloading a file is easy enough. 上传和下载文件非常简单。 Doing so while displaying progress and a transfer rate has me questioning the best way to do it. 在显示进度和传输速率时这样做让我质疑最佳方法。 I'm looking for that one perfect way. 我正在寻找那种完美的方式。 The method that's easy to use, accurate, and light weight. 该方法易于使用,准确,重量轻。 Tried and true. 试过并且是真实的。

The situation I'm talking about is, for example, downloading a file from a WebResponse or some other stream based transfer. 我正在谈论的情况是,例如,从WebResponse或其他一些基于流的传输下载文件。 Downloading it is as simple as (4.0 here): 下载它就像(4.0这里)一样简单:

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream bufferedresponse = new BufferedStream(response.GetResponseStream());
bufferedresponse.CopyTo(TargetStream);
bufferedresponse.Close();

You can't find a transfer rate until after it's done downloading, and even that is just an average. 在完成下载之前,您无法找到传输速率,即使这只是一个平均值。 I'm thinking a touch more instantaneous. 我想的是一瞬间的触摸。 I don't always need an entire WebBrowser object, especially in REST API wrappers (where this particular sample is from), besides, doesn't WebBrowser also attempt to parse DOM and render it's content? 我并不总是需要一个完整的WebBrowser对象,特别是在REST API包装器中(这个特定的示例来自),此外,WebBrowser是否也尝试解析DOM并呈现它的内容?

Are there binding techniques or simple multi-threaded approaches to this? 是否有绑定技术或简单的多线程方法? Are there attributes/events of a stream class I'm overlooking? 我忽略了流类的属性/事件吗? Another class entirely? 完全是另一类?

You need to create an "ObservableStream" that will fire subscribable events that you can handle by incrementing a progress bar and updating telemetry. 您需要创建一个“ObservableStream”,它将通过递增进度条和更新遥测来触发您可以处理的可订阅事件。

Try this implementation, using the Reactive Extensions library: http://staceyw.posterous.com/observe-a-network-stream-using-rx 使用Reactive Extensions库尝试此实现: http//staceyw.posterous.com/observe-a-network-stream-using-rx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM