错误在哪?idhttp中实现多线程(46分)

  • 主题发起人 主题发起人 一头驴
  • 开始时间 开始时间

一头驴

Unregistered / Unconfirmed
GUEST, unregistred user!
unit1:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, IdAntiFreezeBase, IdAntiFreeze,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Memo1: TMemo;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
IdHTTP1: TIdHTTP;
IdAntiFreeze1: TIdAntiFreeze;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Count : Integer;
procedure ThreadDone(Sender: TObject);
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses Unit2;
var
gt : array[1..4] of gethtml;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer;
str_url : string;
begin
Count := 0;
str_url := 'http://www.newjobs.com.cn/qiuzhiguwen/job.jsp?num=60347';
for i := 1 to 4do
begin
gt.Create(str_url);
gt.OnTerminate := ThreadDone;
end;
end;

procedure TForm1.ThreadDone(Sender: TObject);
begin
Inc(Count);
Memo1.Lines.Add('当前完成线程数:'+IntToStr(Count));
end;

end.

unit2:
unit Unit2;
interface
uses
IdHTTP, IdTCPConnection, IdTCPClient, Classes, Dialogs, Graphics, Controls,
SysUtils, Windows, Messages, Variants, StdCtrls;
type
gethtml = class(TThread)
private
{ Private declarations }
furl:string;
protected
procedure Execute;
override;
public
constructor Create(url:string);
end;

implementation
uses Unit1;
constructor gethtml.Create(url:string);
begin
inherited Create(FALSE);
furl:= url;
end;

procedure gethtml.Execute;
var
st: TStringStream;
IdHTTP: TIdHTTP;
begin
st := TStringStream.Create('');
ReturnValue := 10000;
IdHTTP := TIdHTTP.Create(nil);
IdHTTP.HandleRedirects := True;
IdHTTP.ReadTimeout := 60000;
try
IdHTTP.Get(furl,st);
Form1.Memo1.Text := st.DataString;
//FiState^ := True;
except
//FiState^ := False;
end;

IdHTTP.Free;
st.Free;
inherited;
end;

end.
报错,不得要领,望解答.
 
for i := 1 to 4do
begin
[red]gt := gethtml.Create(str_url);[/red]
gt.OnTerminate := ThreadDone;
end;
 
问题解决.
现在出现新的问题:
str_url := 'http://www.newjobs.com.cn/qiuzhiguwen/job.jsp?num=60347';
我想通过循环动态生成str_url
var
i : integer;
begin
for i := 10000 to 20000do
str_url := 'http://www.newjobs.com.cn/qiuzhiguwen/job.jsp?num='+inttostr(i);
end
但如果直接这样写,
for i := 1 to 4do
begin
gt.Create(str_url);
gt.OnTerminate := ThreadDone;
end;
中的gt.Create(str_url);会出现问题吗??
或者说:如何实现对多个类似网页的访问??
 
这样可以吗?
for i:=0 to 3do
begin
str_url := 'http://www.newjobs.com.cn/qiuzhiguwen/job.jsp?num='+inttostr(10000+i);
gt.Create(str_url);
gt.OnTerminate := ThreadDone;
end;
 
to payer:
不行啊,
var
j : integer;
begin
for j := 10000 to 20000do
str_url := 'http://www.newjobs.com.cn/qiuzhiguwen/job.jsp?num='+inttostr(j);
end
for i := 1 to 4do
begin
gt.Create(str_url);
gt.OnTerminate := ThreadDone;
end;
 
循环做得不对吧,你是先定义了 10000-20000 的 str_url
然后再做线程,不等于只做了最后一个 str_url 吗?
 
是双重循环啊。。。
 
后退
顶部