Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件下载的带宽限制_C#_Asp.net_Multithreading - Fatal编程技术网

C# 文件下载的带宽限制

C# 文件下载的带宽限制,c#,asp.net,multithreading,C#,Asp.net,Multithreading,我有以下代码来限制应用程序的文件下载速度 context.Response.Buffer = false; context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + arquivo.Nome); context.Response.AppendHeader("Content-Type",

我有以下代码来限制应用程序的文件下载速度

context.Response.Buffer = false;
context.Response.AppendHeader("Content-Disposition",
                              "attachment;filename=" + arquivo.Nome);
context.Response.AppendHeader("Content-Type",
                              "application/octet-stream");
context.Response.AppendHeader("Content-Length",
                               arquivo.Tamanho.ToString());

int offset = 0;
byte[] buffer = new byte[currentRange.OptimalDownloadRate];

while (context.Response.IsClientConnected && offset < arquivo.Tamanho)
{
    DateTime start = DateTime.Now;
    int readCount = arquivo.GetBytes(buffer, offset, // == .ExecuteReader()
        (int)Math.Min(arquivo.Tamanho - offset, buffer.Length));
    context.Response.OutputStream.Write(buffer, 0, readCount);
    offset += readCount;
    CacheManager.Hit(jobId, fileId.ToString(), readCount, buffer.Length, null);

    TimeSpan elapsed = DateTime.Now - start;
    if (elapsed.TotalMilliseconds < 1000)
    {
        Thread.Sleep(1000 - (int)elapsed.TotalMilliseconds);
    }
}
对于用户,下载对话框时会打开一个新窗口:

The connection with the server was reset

你知道发生了什么吗?

这可能是因为在其上运行的IIS假定您的web应用程序挂起,因为它的线程在睡眠时无法访问。然后它回收工作线程


你应该尽量缩短睡眠时间。1秒似乎很高…

问题是请求运行了90秒以上


我将修改HTTP处理程序以实现
IHttpAsyncHandler
,并创建一个后台非阻塞线程。现在一切正常。

我没有答案,但这似乎很常见:
The connection with the server was reset