Hiv - 1

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type

TMyThread = class(TThread)
  private
    { Private declarations }
    procedure AddTh;
    procedure DecTh;
  protected
    procedure Execute; override;
  end;

  TForm1 = class(TForm)
    Edit1: TEdit;
    Timer1: TTimer;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ThCounter:integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ThCounter:=0;
end;

{ TMyThread }

procedure TMyThread.AddTh;
begin
  Form1.ThCounter:= Form1.ThCounter +1;
  Form1.Edit1.Text:= intToStr(Form1.ThCounter);
end;
procedure TMyThread.DecTh;
begin
  Form1.ThCounter:= Form1.ThCounter -1;
  Form1.Edit1.Text:= intToStr(Form1.ThCounter);
end;
procedure TMyThread.Execute;
var
  i:integer;
begin
  inherited;
  Synchronize(AddTh);
  sleep(10000);

  Synchronize(DecTh);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  th:TMyThread;
begin
if(Form1.ThCounter<5) then
begin
  th:= TMyThread.Create(true);
  th.FreeOnTerminate:=true;
  th.Resume();
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled:=true;
end;

end.
posted @ 2011-06-23 19:57  noevil  阅读(200)  评论(0编辑  收藏  举报