一个多线程刷页面的程序

//方法就是用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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值