下载管理器 - 限制下载速度 [英] Download manager - limit download speed

查看:373
本文介绍了下载管理器 - 限制下载速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行一个下载速率限制我的文件下载的应用程序,我已经看了从$的 ThrottledStream C类$的CProject,但这不会在我的情况下工作,因为我必须要能够在下载过程中改变了极限,不只是在开始。下面是我用在后台线程下载方法的一部分:

I need to implement a download rate limit for my file downloader application, and I've looked at the ThrottledStream class from CodeProject, but this won't work in my case since I have to be able to change the limit during a download, not just at the beginning. Here's a part of the download method that I'm using in a background thread:

webResponse = (HttpWebResponse)webRequest.GetResponse();
responseStream = webResponse.GetResponseStream();
responseStream.ReadTimeout = 5000;

downloadCache = new MemoryStream(this.MaxCacheSize);
byte[] downloadBuffer = new byte[this.BufferSize];
int bytesSize = 0;
CachedSize = 0;
int receivedBufferCount = 0;

while (true)
{
    bytesSize = responseStream.Read(downloadBuffer, 0, downloadBuffer.Length);

    if (this.Status != DownloadStatus.Downloading || bytesSize == 0 
        || this.MaxCacheSize < CachedSize + bytesSize)
    {
        WriteCacheToFile(downloadCache, CachedSize);

        this.DownloadedSize += CachedSize;
        downloadCache.Seek(0, SeekOrigin.Begin);
        CachedSize = 0;

        if (this.Status != DownloadStatus.Downloading || bytesSize == 0)
            break;
    }

    downloadCache.Write(downloadBuffer, 0, bytesSize);
    CachedSize += bytesSize;

    receivedBufferCount++;
    if (receivedBufferCount == this.BufferCountPerNotification)
    {
        this.RaiseDownloadProgressChanged();
        receivedBufferCount = 0;
    }
}

我也看到有人使用Thread.sleep()方法或Thread.Wait(),但它是一个好主意吗?你有什么建议,我怎么能做到这一点,而这个循环中?

I've also seen people using Thread.Sleep() or Thread.Wait(), but is it a good idea? Do you have any suggestions how I could do this inside this while loop?

推荐答案

您正在寻找所谓的带宽限制这里是一个很好的例子 这是怎么做的,还查看评论就知道它是如何从客户端完成一面。

What you are looking for is called Bandwidth throttling And here is a good example how is this done, also review the comments to know how it is done from a client side.

您可能还想看看 这个例子 太多,把事情在实际应用

You may also want to take a look at this example too, putting things in a real application

修改我: 我可以调整上飞比特率? 是的,您可以:)

EDIT I : Can I adjust the bit rate on the fly ? Yes, you can :)

这篇关于下载管理器 - 限制下载速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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