Z
zzandyzh
Unregistered / Unconfirmed
GUEST, unregistred user!
//线程单元
unit Mythread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IdHTTP;
type
FThread = class(TThread)
private
idhttp:tidhttp;
number:integer;
url:string;
zt:string;
PaR:string;
protected
Procedure seturl;{ Private declarations }
procedure addzt;
procedure addwz;
procedure Execute;
override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy;override;
end;
implementation
uses unit1;
constructor Fthread.Create(CreateSuspended: Boolean);
begin
IdHttp:=TIdHttp.Create(nil);
idhttp.ConnectTimeout:=20000;
Inherited Create(CreateSuspended);
FreeOnTerminate:=True;//当线程终止时自动销毁线程对象
end;
destructor Fthread.Destroy;
begin
IdHttp.Disconnect;
IdHttp.Free;
inherited;
end;
procedure Fthread.seturl;
var
i:integer;
begin
url:=form1.memo1.Lines.Strings[minnum];
minnum:=minnum+1;
form1.statusbar1.Panels.Items[1].Text:='已检测'+inttostr(minnum)+'个地址' ;
form1.ProgressBar1.Position:=minnum;
end;
procedure Fthread.addzt;
var
aa:integer;
begin
if trim(par)<>'0' then
form1.Memo13.Text:=form1.Memo13.Text+#13#10+(url);
try
aa:=strtoint(trim(par)) ;
except
beep
end;
case aa of
0: form1.Memo3.text:=form1.Memo3.Text+#13#10+(url);
1: form1.Memo2.text:=form1.Memo2.Text+#13#10+(url);
2: form1.Memo4.text:=form1.Memo4.Text+#13#10+(url);
3: form1.Memo5.text:=form1.Memo5.Text+#13#10+(url);
4: form1.Memo6.text:=form1.Memo6.Text+#13#10+(url);
5: form1.Memo7.text:=form1.Memo7.Text+#13#10+(url);
6: form1.Memo8.text:=form1.Memo8.Text+#13#10+(url);
7: form1.Memo9.text:=form1.Memo9.Text+#13#10+(url);
8: form1.Memo10.text:=form1.Memo10.Text+#13#10+(url);
9: form1.Memo11.text:=form1.Memo11.Text+#13#10+(url);
10: form1.Memo12.text:=form1.Memo12.Text+#13#10+(url);
end;
end;
procedure Fthread.addwz;
begin
form1.memo14.Text:=form1.memo14.Text+#13#10+(url);
end;
procedure fThread.Execute;
const
baseurl='http://soft.baofe.com/k.asp?url=' ;
var
html:string;
Params: TStrings;
p1,p2: Integer;
begin
while (not terminated)and(minnum<=maxnum)do
begin
application.ProcessMessages ;
sleep(1);
if form1.CheckBox1.Checked then
begin
idhttp.ProxyParams.ProxyServer:=form1.Edit5.Text;
idhttp.ProxyParams.ProxyPort:=strtoint(form1.Edit6.text);
end;
Synchronize(seturl);
try
HTML:=IdHttp.get(BaseURL+url);
except
synchronize(addwz);
idhttp.Disconnect;
end;
p1:=pos('style',html);
par:=copy(html,p1+length('red'),1);
synchronize(addzt);
end;
end;
end.
//调用单元
procedure TForm1.Button1Click(Sender: TObject);
var
j,maxthread:integer;
begin
isend:=false;
progressbar1.Min:=0;
progressbar1.Max:=memo1.Lines.Count;
progressbar1.Step:=1;
//button1.Enabled :=true;
maxthread:=strtoint(edit1.text);
minnum:=0 ;
maxnum:=memo1.Lines.Count-1;
for j:=0 to maxthread-1do
begin
n:=Fthread.Create(true) ;
n.FreeOnTerminate:=true;
n.Resume;
end;
end;
程序运行时Cpu占用了100%,而且运行不到一小时,程序运行的很慢,比不用多线程还慢,
请大家给看看!
还有就是怎么才能暂停、继续、停止线程!
下边是我的暂停、继续和停止代码,但不起任何作用!
procedure TForm1.Button3Click(Sender: TObject);
begin
if button3.Caption='暂停' then
begin
button3.Caption :='继续';
if n.Suspended then
begin
n.Resume ;
end;
end
else
begin
button3.Caption :='暂停';
if n.Suspended=false then
begin
n.Suspend;
end;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
n.Terminate;
end
unit Mythread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IdHTTP;
type
FThread = class(TThread)
private
idhttp:tidhttp;
number:integer;
url:string;
zt:string;
PaR:string;
protected
Procedure seturl;{ Private declarations }
procedure addzt;
procedure addwz;
procedure Execute;
override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy;override;
end;
implementation
uses unit1;
constructor Fthread.Create(CreateSuspended: Boolean);
begin
IdHttp:=TIdHttp.Create(nil);
idhttp.ConnectTimeout:=20000;
Inherited Create(CreateSuspended);
FreeOnTerminate:=True;//当线程终止时自动销毁线程对象
end;
destructor Fthread.Destroy;
begin
IdHttp.Disconnect;
IdHttp.Free;
inherited;
end;
procedure Fthread.seturl;
var
i:integer;
begin
url:=form1.memo1.Lines.Strings[minnum];
minnum:=minnum+1;
form1.statusbar1.Panels.Items[1].Text:='已检测'+inttostr(minnum)+'个地址' ;
form1.ProgressBar1.Position:=minnum;
end;
procedure Fthread.addzt;
var
aa:integer;
begin
if trim(par)<>'0' then
form1.Memo13.Text:=form1.Memo13.Text+#13#10+(url);
try
aa:=strtoint(trim(par)) ;
except
beep
end;
case aa of
0: form1.Memo3.text:=form1.Memo3.Text+#13#10+(url);
1: form1.Memo2.text:=form1.Memo2.Text+#13#10+(url);
2: form1.Memo4.text:=form1.Memo4.Text+#13#10+(url);
3: form1.Memo5.text:=form1.Memo5.Text+#13#10+(url);
4: form1.Memo6.text:=form1.Memo6.Text+#13#10+(url);
5: form1.Memo7.text:=form1.Memo7.Text+#13#10+(url);
6: form1.Memo8.text:=form1.Memo8.Text+#13#10+(url);
7: form1.Memo9.text:=form1.Memo9.Text+#13#10+(url);
8: form1.Memo10.text:=form1.Memo10.Text+#13#10+(url);
9: form1.Memo11.text:=form1.Memo11.Text+#13#10+(url);
10: form1.Memo12.text:=form1.Memo12.Text+#13#10+(url);
end;
end;
procedure Fthread.addwz;
begin
form1.memo14.Text:=form1.memo14.Text+#13#10+(url);
end;
procedure fThread.Execute;
const
baseurl='http://soft.baofe.com/k.asp?url=' ;
var
html:string;
Params: TStrings;
p1,p2: Integer;
begin
while (not terminated)and(minnum<=maxnum)do
begin
application.ProcessMessages ;
sleep(1);
if form1.CheckBox1.Checked then
begin
idhttp.ProxyParams.ProxyServer:=form1.Edit5.Text;
idhttp.ProxyParams.ProxyPort:=strtoint(form1.Edit6.text);
end;
Synchronize(seturl);
try
HTML:=IdHttp.get(BaseURL+url);
except
synchronize(addwz);
idhttp.Disconnect;
end;
p1:=pos('style',html);
par:=copy(html,p1+length('red'),1);
synchronize(addzt);
end;
end;
end.
//调用单元
procedure TForm1.Button1Click(Sender: TObject);
var
j,maxthread:integer;
begin
isend:=false;
progressbar1.Min:=0;
progressbar1.Max:=memo1.Lines.Count;
progressbar1.Step:=1;
//button1.Enabled :=true;
maxthread:=strtoint(edit1.text);
minnum:=0 ;
maxnum:=memo1.Lines.Count-1;
for j:=0 to maxthread-1do
begin
n:=Fthread.Create(true) ;
n.FreeOnTerminate:=true;
n.Resume;
end;
end;
程序运行时Cpu占用了100%,而且运行不到一小时,程序运行的很慢,比不用多线程还慢,
请大家给看看!
还有就是怎么才能暂停、继续、停止线程!
下边是我的暂停、继续和停止代码,但不起任何作用!
procedure TForm1.Button3Click(Sender: TObject);
begin
if button3.Caption='暂停' then
begin
button3.Caption :='继续';
if n.Suspended then
begin
n.Resume ;
end;
end
else
begin
button3.Caption :='暂停';
if n.Suspended=false then
begin
n.Suspend;
end;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
n.Terminate;
end