我是回圈下載很多網路檔案 ,但是下載程序中 會經常卡死不動 而且沒有例外拋出 。主執行緒放IdAntiFreeze1 或者下載部分放 單獨執行緒也會卡死。
IdHTTP 是阻塞的 有沒有好的替代方案
var
IdHTTP: TIdHttp;
MemStream: TMemoryStream;
UrlStr, DFileName: string;
FilePosition: int64;
FileSize: Integer;
begin
IdHTTP := TIdHttp.Create(nil);
IdHTTP.ProtocolVersion := pv1_1;
// IdHttp.RecvBufferSize := 1024 ;
IdHTTP.HandleRedirects := true;
IdHTTP.ConnectTimeout :=60000;
IdHTTP.ReadTimeout :=60000;
IdAntiFreeze1.OnlyWhenIdle:=False;//設定使程式有反應.
lbl_error.Caption:='0';
tmr_dwonerror.Enabled :=true;
try
// 獲取重定向后的下載地址和檔案大小
IdHTTP.Head(FASrcURL);
FileSize := IdHTTP.Response.ContentLength;
if FileSize <= 0 then
exit;
{
DFileName := idhttp.URL.URI;
//由下載地址轉換為保存檔案名
while pos('/',DFileName) > 0 do
begin
delete(DFileName, 1, pos('/',DFileName));
end;
while pos('/',DFileName) > 0 do
begin
delete(DFileName, 1, pos('/',DFileName));
end;
if DFileName = '' then
exit;
}
FilePosition := 0;
MemStream := TMemoryStream.Create;
cxProgressBar1.Properties.Max :=FileSize;
while FilePosition < FileSize do
begin
// 方法1:未驗證
// IdHttp.Request.ContentRangeStart := FilePosition;
// IdHttp.Request.ContentRangeEnd := FilePosition + 1024;
// 方法2:已驗證
try
IdHTTP.Request.Range := IntToStr(FilePosition) + '-';
if FilePosition + 10240 < FileSize then
IdHTTP.Request.Range := IdHTTP.Request.Range +
IntToStr(FilePosition + 10239);
IdHTTP.get(IdHTTP.URL.URI, MemStream);
// MemStream.LoadFromStream(IdHttp.Response.ContentStream);
MemStream.SaveToFile(FASaveFileName);
FilePosition := MemStream.Size;
cxProgressBar1.Position := FilePosition;
Application.ProcessMessages;
except
exit;
end;
// sleep(50);
end;
tmr_dwonerror.Enabled :=false;
Result := true;
finally
IdHTTP.Free;
MemStream.Free;
cxProgressBar1.Position:=0;
end;
uj5u.com熱心網友回復:
樓主用什么版本D:\Users\Public\Documents\Embarcadero\Studio\16.0\Samples\Object Pascal\RTL\HttpDownload
這里有個官方的例子,多執行緒,挺不錯的!
uj5u.com熱心網友回復:
http://docwiki.embarcadero.com/CodeExamples/XE8/en/RTL.HttpDownload_Sampleuj5u.com熱心網友回復:
ICS8也不錯。uj5u.com熱心網友回復:

也是IdHTTP Get 自動升級用
uj5u.com熱心網友回復:
我用的 xe2 + indy 10uj5u.com熱心網友回復:
不行的話,采取多執行緒。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/82153.html
標籤:VCL組件開發及應用
