下载文件已经用idHttp解决了,但是下载个大的文件耗时很长,占资源太多,机器都不动了。 我用idHttp包装在TThread里执行,仍然速度很慢,CPU占用太多,像死机了似的,有什么好办法阿? 高手看看:Procedure _DLThread.Execute; Var FS:TFileStream; idHttpR:TidHttp; sInfo, sURL,sFile:String; Begin if Suspended then Begin //Suspend; Exit; End; sURL:=sDownLoadURL; sFile:=sTargetFile; // if (sUrl='') or (sFile='') then Exit; // With Frm_MDI do //,Frm_MDI.idHttp1 Try sInfo:='下载文件['+sURL+']到['+sFile+']'; ShowInfo('开始'+sInfo+'... ...'); if FileExists(sFile) then DeleteFile(sFile); //句柄属于线程 idHttp1:=tIDHttp.Create(Application); // Try FS:=TFileStream.Create(sFile,fmCreate); //文件流 idHttpR.Get(sURL,FS); //下载 // ShowInfo(sInfo+'完毕'); Except On E:Exception do Begin ShowInfo('[DownLoadHttpFile]'+sInfo+'失败'+#13+E.message); ShowMessage('[DownLoadHttpFile]'+sInfo+'出错!'+#13+E.message); End; End; // Finally If Assigned(FS) then FS.Free; if Assigned(idHttpR) then idHttpR.Free; End; //Suspended:=True; End;
有知道的继续发: [email protected] 送分。谢谢各位。
Uses UrlMon;
DownloadURLToFilehttp://lysoft.7u7.net
Var MyStream : TMemoryStream;
ErrorCode : HResult;
begin
MyStream := TMemoryStream.Create;
try
ErrorCode:= DownloadURL('http://www.steema.com/demo.txt',MyStream);
if ErrorCode=0 then
Memo1.Lines.LoadFromStream(MyStream)
else
ShowMessage( TeeURLErrorMessage( ErrorCode ) );
finally
MyStream.Free;
end;end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TeeURL;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var MyStream : TMemoryStream;
ErrorCode : HResult;
begin
MyStream := TMemoryStream.Create;
try
ErrorCode:= DownloadURL('http://www.steema.com/demo.txt',MyStream);
if ErrorCode=0 then
Memo1.Lines.LoadFromStream(MyStream)
else
ShowMessage( TeeURLErrorMessage( ErrorCode ) );
finally
MyStream.Free;
end;end;
end.
我用idHttp包装在TThread里执行,仍然速度很慢,CPU占用太多,像死机了似的,有什么好办法阿?
高手看看:Procedure _DLThread.Execute;
Var FS:TFileStream;
idHttpR:TidHttp;
sInfo, sURL,sFile:String;
Begin
if Suspended then Begin
//Suspend;
Exit;
End;
sURL:=sDownLoadURL;
sFile:=sTargetFile;
//
if (sUrl='') or (sFile='') then Exit;
//
With Frm_MDI do //,Frm_MDI.idHttp1
Try
sInfo:='下载文件['+sURL+']到['+sFile+']';
ShowInfo('开始'+sInfo+'... ...');
if FileExists(sFile) then DeleteFile(sFile);
//句柄属于线程 idHttp1:=tIDHttp.Create(Application);
//
Try
FS:=TFileStream.Create(sFile,fmCreate); //文件流
idHttpR.Get(sURL,FS); //下载
//
ShowInfo(sInfo+'完毕');
Except
On E:Exception do Begin
ShowInfo('[DownLoadHttpFile]'+sInfo+'失败'+#13+E.message);
ShowMessage('[DownLoadHttpFile]'+sInfo+'出错!'+#13+E.message);
End;
End;
//
Finally
If Assigned(FS) then FS.Free;
if Assigned(idHttpR) then idHttpR.Free;
End;
//Suspended:=True;
End;
需要APIHook,NT Ring0等的才去我那里找的http://lysoft.7u7.net