2009-10-15 3 views
1

응용 프로그램의 파일 다운로드 속도를 제한하는 코드를 다음과 같이 사용했습니다. 언제나 파일 다운로드에 대한 대역폭 조절

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); 
    } 
} 

, 그것은 내 개발, 내부 고객 QA 환경에 잘 작동하지만 프로덕션 환경에서 예외를 던지고 :

사용자에 대해
System.Threading.ThreadAbortException: Thread was being aborted. 
    at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout) 
    at (...).Handlers.DownloadHandler.processDownload(HttpContext context, ...) 

새 창 다운로드 대화 상자에 열립니다

The connection with the server was reset 

무슨 일이 일어나는지 알고 있니?

+1

답변이 없지만 일반적으로 다음과 같이 보입니다. http://www.google.com/search?q=SleepInternal+threadabortexception – asveikau

답변

1

요청이 90 초 이상 실행되는 문제가있었습니다.

IHttpAsyncHandler을 구현하고 비 블로킹 스레드를 생성하려면 HTTP 처리기를 변경하십시오. 이제 모든 것이 잘됩니다.

1

IIS가 실행되는 것은 웹 응용 프로그램이 잠자기 중에 연결할 수 없으므로 중단된다고 가정 할 수 있습니다. 그런 다음 작업자 스레드를 재활용합니다.

단순히 수면 간격을 줄여야합니다. 1 초가 높음 ...

+0

+1 : 아이디어입니다. 시도해 보겠습니다. –

관련 문제