//方法就是用5个线程不停的get页面,本来想用access但是发现5个线程常常有的会挂起,于是选择了用文本文件存储待刷的页面地址.主要控件就是IdHTTP这个控件,我在本站前面的文章中已经描述了他的基本用法.这个例子虽然简单,但是稍加改动就可以增加动态IP功能,并做成论坛发贴机之类的东东.
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls,StrUtils, ExtCtrls, DB, ADODB;
type //上行线程
TGetThread = class(TThread)
protected
index:integer;
procedure Execute; override;
public
constructor Create(CreateSuspended:boolean);
end;
type
TForm1 = class(TForm)
IdHTTP5: TIdHTTP;
Button1: TButton;
Edit1: TEdit;
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
Label5: TLabel;
Edit5: TEdit;
IdHTTP1: TIdHTTP;
IdHTTP2: TIdHTTP;
IdHTTP3: TIdHTTP;
IdHTTP4: TIdHTTP;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//5个全局变量存储着每个线程的已经访问的页面计数,他会显示在各自的edit中.
count1:integer=0;
count2:integer=0;
count3:integer=0;
count4:integer=0;
count5:integer=0;
threadIndex:integer=0;
send1,send2,send3,send4,send5:TGetThread;
//url文本文件的地址将会读到下边这个stringlist中.
urlList:TStringList;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
//10
send1.Resume;
send2.Resume;
send3.Resume;
send4.Resume;
send5.Resume;
end;
{ TGetThread }
constructor TGetThread.Create(CreateSuspended: boolean);
begin
//
inherited;
Self.index:=threadIndex;
end;
procedure TGetThread.Execute;
begin
//
while not Terminated do
begin
if Self.index=1 then
begin
Form1.IdHTTP1.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count1 mod urlList.Count]<>'') then
Form1.IdHTTP1.Get(urlList.Strings[count1 mod urlList.Count]);
count1:=count1+1;
end
else if Self.index=2 then
begin
Form1.IdHTTP2.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count2 mod urlList.Count]<>'') then
Form1.IdHTTP2.Get(urlList.Strings[count2 mod urlList.Count]);
count2:=count2+1;
end
else if Self.index=3 then
begin
Form1.IdHTTP3.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count3 mod urlList.Count]<>'') then
Form1.IdHTTP3.Get(urlList.Strings[count3 mod urlList.Count]);
count3:=count3+1;
end
else if Self.index=4 then
begin
Form1.IdHTTP4.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count4 mod urlList.Count]<>'') then
Form1.IdHTTP4.Get(urlList.Strings[count4 mod urlList.Count]);
count4:=count4+1;
end
else if Self.index=5 then
begin
Form1.IdHTTP5.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count5 mod urlList.Count]<>'') then
Form1.IdHTTP5.Get(urlList.Strings[count5 mod urlList.Count]);
count5:=count5+1;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Edit1.Text:=IntToStr(count1);
Edit2.Text:=IntToStr(count2);
Edit3.Text:=IntToStr(count3);
Edit4.Text:=IntToStr(count4);
Edit5.Text:=IntToStr(count5);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
try
begin
send1.Terminate;
send2.Terminate;
send3.Terminate;
send4.Terminate;
send5.Terminate;
end;
except
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
threadIndex:=0;
threadIndex:=threadIndex+1;
send1:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send2:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send3:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send4:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send5:=TGetThread.Create(true);
urlList:=TStringList.Create;
urlList.LoadFromFile(ExtractFilePath(Paramstr(0))+'url.txt');
showmessage('页面个数:'+IntToStr(urlList.Count));
end;
end.
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls,StrUtils, ExtCtrls, DB, ADODB;
type //上行线程
TGetThread = class(TThread)
protected
index:integer;
procedure Execute; override;
public
constructor Create(CreateSuspended:boolean);
end;
type
TForm1 = class(TForm)
IdHTTP5: TIdHTTP;
Button1: TButton;
Edit1: TEdit;
Timer1: TTimer;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
Label5: TLabel;
Edit5: TEdit;
IdHTTP1: TIdHTTP;
IdHTTP2: TIdHTTP;
IdHTTP3: TIdHTTP;
IdHTTP4: TIdHTTP;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//5个全局变量存储着每个线程的已经访问的页面计数,他会显示在各自的edit中.
count1:integer=0;
count2:integer=0;
count3:integer=0;
count4:integer=0;
count5:integer=0;
threadIndex:integer=0;
send1,send2,send3,send4,send5:TGetThread;
//url文本文件的地址将会读到下边这个stringlist中.
urlList:TStringList;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
//10
send1.Resume;
send2.Resume;
send3.Resume;
send4.Resume;
send5.Resume;
end;
{ TGetThread }
constructor TGetThread.Create(CreateSuspended: boolean);
begin
//
inherited;
Self.index:=threadIndex;
end;
procedure TGetThread.Execute;
begin
//
while not Terminated do
begin
if Self.index=1 then
begin
Form1.IdHTTP1.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count1 mod urlList.Count]<>'') then
Form1.IdHTTP1.Get(urlList.Strings[count1 mod urlList.Count]);
count1:=count1+1;
end
else if Self.index=2 then
begin
Form1.IdHTTP2.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count2 mod urlList.Count]<>'') then
Form1.IdHTTP2.Get(urlList.Strings[count2 mod urlList.Count]);
count2:=count2+1;
end
else if Self.index=3 then
begin
Form1.IdHTTP3.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count3 mod urlList.Count]<>'') then
Form1.IdHTTP3.Get(urlList.Strings[count3 mod urlList.Count]);
count3:=count3+1;
end
else if Self.index=4 then
begin
Form1.IdHTTP4.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count4 mod urlList.Count]<>'') then
Form1.IdHTTP4.Get(urlList.Strings[count4 mod urlList.Count]);
count4:=count4+1;
end
else if Self.index=5 then
begin
Form1.IdHTTP5.Request.ContentType :='application/x-www-form-urlencoded';
if (urlList.Strings[count5 mod urlList.Count]<>'') then
Form1.IdHTTP5.Get(urlList.Strings[count5 mod urlList.Count]);
count5:=count5+1;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Edit1.Text:=IntToStr(count1);
Edit2.Text:=IntToStr(count2);
Edit3.Text:=IntToStr(count3);
Edit4.Text:=IntToStr(count4);
Edit5.Text:=IntToStr(count5);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
try
begin
send1.Terminate;
send2.Terminate;
send3.Terminate;
send4.Terminate;
send5.Terminate;
end;
except
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
threadIndex:=0;
threadIndex:=threadIndex+1;
send1:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send2:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send3:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send4:=TGetThread.Create(true);
threadIndex:=threadIndex+1;
send5:=TGetThread.Create(true);
urlList:=TStringList.Create;
urlList.LoadFromFile(ExtractFilePath(Paramstr(0))+'url.txt');
showmessage('页面个数:'+IntToStr(urlList.Count));
end;
end.