Necesito implementar un límite de velocidad de descarga para mi aplicación de descarga de archivos, y he visto la clase ThrottledStream
de CodeProject, pero esto no funcionará en mi caso ya que tengo que poder para cambiar el límite durante una descarga, no solo al principio. He aquí una parte del método de descarga que estoy usando en un subproceso de fondo:Administrador de descargas: limite la velocidad de descarga
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;
}
}
También he visto personas que utilizan Thread.Sleep() o Thread.Wait(), pero es una buena idea? ¿Tienes alguna sugerencia de cómo podría hacer esto dentro de este ciclo while?
Necesito ver método CalcFileSize – mehrdad