关于多线程上载总是有失败的困扰,请高手帮忙看看代码有什么有关问题(100分)
关于多线程下载总是有失败的困扰,请高手帮忙看看代码有什么问题(100分)。
以下是具体的代码实现,请高手看看有没有需要改进的地方。谢谢!
以下是具体的代码实现,请高手看看有没有需要改进的地方。谢谢!
procedure TDownload.Execute;
var
idHTTP : TIdHTTP;
isExcetpion:Boolean;
urlindex:integer;
url1:string;
begin
urlindex:=aManager.ThisJob.iUrlIndex;
iLeft := aManager.arDLFThread[iIndex].iOffsetEnd - iThisBegin;
aManager.arThisDownloadSize[iIndex] := 0;
Repeat
begin
isExcetpion:= true;
idHTTP := TidHTTP.Create(nil);
try
iThisBegin := aManager.arDLFThread[iIndex].iOffsetBegin + aManager.arDLFThread[iIndex].iOffsetDownload;
idHTTP.HandleRedirects := True;
idHTTP.ReadTimeout := 30000;
idHTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Maxthon)';
idHTTP.Request.Connection:='Keep-Alive';
idHTTP.Request.ContentRangeStart := iThisBegin;
idHTTP.Request.ContentRangeEnd := aManager.arDLFThread[iIndex].iOffsetEnd;
url1:=aManager.thisJob.iUrls[urlindex];
idHTTP.DoRequest(hmGet, url1, nil, nil);
while not aManager.arStop[iIndex] and (iLeft > 0) do
if iLeft > BufferSize then
begin
idHTTP.ReadBuffer(Buffer, BufferSize);
fs.WriteBuffer(Buffer, BufferSize);
aManager.arDLFThread[iIndex].iOffsetDownload := aManager.arDLFThread[iIndex].iOffsetDownload + BufferSize;
aManager.arThisDownloadSize[iIndex] := aManager.arThisDownloadSize[iIndex] + BufferSize;
iLeft := iLeft - BufferSize;
aManager.arDLFThread[iIndex].iState := 1;
end
else
begin
idHTTP.ReadBuffer(Buffer, iLeft);
fs.WriteBuffer(Buffer, iLeft);
aManager.arDLFThread[iIndex].iOffsetDownload := aManager.arDLFThread[iIndex].iOffsetDownload + iLeft;