gpt4 book ai didi

c# - 下载管理器 - 限制下载速度

转载 作者:太空狗 更新时间:2023-10-29 23:47:26 25 4
gpt4 key购买 nike

<分区>

我需要为我的文件下载器应用程序实现下载速率限制,并且我查看了 CodeProject 中的 ThrottledStream 类,但这在我的情况下不起作用,因为我必须能够在下载期间更改限制,而不仅仅是在开始时。这是我在后台线程中使用的下载方法的一部分:

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(),但这是个好主意吗?您对我如何在这个 while 循环中执行此操作有什么建议吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com