S
shmily0616
Unregistered / Unconfirmed
GUEST, unregistred user!
在主程序里,已经根据关键词生成了正确的url和urllist:tstringlist
在线程里获得返回,同步getresult,在主程序处理结果并显示在memo2
如果我不用多线程,在主程序运行,能得到正确的结果;
可是用了多线程,运行时总是出错。
请大家帮我看看这段线程代码哪里错了?还是变量声明的不对?
我死活找不出错误了。
[blue]------------------------------------------- [/blue]
定义了类:
type
backis = class(TThread)
private
{ Private declarations }
do
mainlist,urllist:tstringlist;
back,start,ends :string;
suff,len :integer;
procedure getresult;
protected
procedure Execute;
override;
public
constructor Create(domainpara,listpara:tstringlist);
destructor Destroy;
Override;
end;
--------------------------------------------------
constructor backis.Create(domainpara,listpara:tstringlist);
begin
inherited Create(FALSE);
do
mainlist :=tstringlist.Create;
urllist :=tstringlist.Create;
do
mainlist :=domainpara;
urllist :=listpara;
end;
--------------------------------------------------
destructor backis.Destroy;
begin
inherited Destroy;
end;
------------------------------------------------------------
procedure backis.Execute;
var
idhttp :tidhttp;
counts,i :integer;
begin
idhttp :=tidhttp.Create(nil);
idhttp.Request.Connection :='close';
idhttp.ConnectTimeout :=10000;
idhttp.ReadTimeout :=10000;
counts :=urllist.Count;
for i :=0 to counts-1do
begin
back :=idhttp.Get(urllist.Strings);
suff :=i;
synchronize(getresult);
end;
idhttp.Disconnect;
idhttp.Free;
end;
--------------------------------------------------------------
procedure backis.getresult;
//这段处理返回结果并显示的代码,我不用多线程的话,是正确的
var
pos1,pos2 :integer;
back1,back2 :string;
begin
start :='有 <b>';
//是不是这3个变量的问题?这3个值都是固定的。
len :=20;
//
ends :=' </b>项符合';
//
pos1 :=pos(start,back);
if pos1 > 0 then
begin
back1 :=copy(back,pos1+length(start),len);
pos2 :=pos(ends,back1);
back2 :=copy(back1,1,pos2-1);
Form1.memo2.Lines.Add(domainlist.Strings[suff]+#09+back2);
end
else
Form1.memo2.Lines.Add(domainlist.Strings[suff]+#09+'0');
end;
在线程里获得返回,同步getresult,在主程序处理结果并显示在memo2
如果我不用多线程,在主程序运行,能得到正确的结果;
可是用了多线程,运行时总是出错。
请大家帮我看看这段线程代码哪里错了?还是变量声明的不对?
我死活找不出错误了。
[blue]------------------------------------------- [/blue]
定义了类:
type
backis = class(TThread)
private
{ Private declarations }
do
mainlist,urllist:tstringlist;
back,start,ends :string;
suff,len :integer;
procedure getresult;
protected
procedure Execute;
override;
public
constructor Create(domainpara,listpara:tstringlist);
destructor Destroy;
Override;
end;
--------------------------------------------------
constructor backis.Create(domainpara,listpara:tstringlist);
begin
inherited Create(FALSE);
do
mainlist :=tstringlist.Create;
urllist :=tstringlist.Create;
do
mainlist :=domainpara;
urllist :=listpara;
end;
--------------------------------------------------
destructor backis.Destroy;
begin
inherited Destroy;
end;
------------------------------------------------------------
procedure backis.Execute;
var
idhttp :tidhttp;
counts,i :integer;
begin
idhttp :=tidhttp.Create(nil);
idhttp.Request.Connection :='close';
idhttp.ConnectTimeout :=10000;
idhttp.ReadTimeout :=10000;
counts :=urllist.Count;
for i :=0 to counts-1do
begin
back :=idhttp.Get(urllist.Strings);
suff :=i;
synchronize(getresult);
end;
idhttp.Disconnect;
idhttp.Free;
end;
--------------------------------------------------------------
procedure backis.getresult;
//这段处理返回结果并显示的代码,我不用多线程的话,是正确的
var
pos1,pos2 :integer;
back1,back2 :string;
begin
start :='有 <b>';
//是不是这3个变量的问题?这3个值都是固定的。
len :=20;
//
ends :=' </b>项符合';
//
pos1 :=pos(start,back);
if pos1 > 0 then
begin
back1 :=copy(back,pos1+length(start),len);
pos2 :=pos(ends,back1);
back2 :=copy(back1,1,pos2-1);
Form1.memo2.Lines.Add(domainlist.Strings[suff]+#09+back2);
end
else
Form1.memo2.Lines.Add(domainlist.Strings[suff]+#09+'0');
end;