我使用此代码读取 HttpWebResponse 流
Stream stm = httpResp.GetResponseStream();
Stream fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
byte[] buff = new byte[1024 * 16];
AsyncCallback callback = null;
callback = ar =>
{
int bytesRead = stm.EndRead(ar);
fs.Write(buff, 0, bytesRead);
if(bytesRead == 0)
return;
stm.BeginRead(buff, 0, buff.Length, callback, null);
};
stm.BeginRead(buff, 0, buff.Length, callback, null);
我想限制下载速率。如果我在回调中放置一个 Thread.Sleep() ,它将使线程保持阻塞,但它不能很好地适应许多流。有没有其他方法可以实现下载限制和线程经济?