Tôi cần triển khai giới hạn tốc độ tải xuống cho ứng dụng trình tải xuống tệp của mình và tôi đã xem lớp ThrottledStream
từ CodeProject, nhưng điều này sẽ không hoạt động trong trường hợp của tôi vì tôi có thể để thay đổi giới hạn trong quá trình tải xuống, không chỉ ở đầu. Dưới đây là một phần của phương pháp tải về mà tôi đang sử dụng trong một thread nền:Trình quản lý tải xuống - giới hạn tốc độ tải xuống
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;
}
}
Tôi đã người ta cũng nhìn thấy sử dụng Thread.Sleep() hoặc Thread.Wait(), nhưng nó là một ý tưởng tốt? Bạn có bất cứ đề nghị làm thế nào tôi có thể làm điều này bên trong vòng lặp này trong khi?
tôi cần phải nhìn thấy phương pháp CalcFileSize – mehrdad