我实验的程序是正常的。最好将你们不正常的程序贴上来。
下面是我实验的程序:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Unit2;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
T:TTryThread;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
T:=TTryThread.Create;
T.Resume;
Button1.Enabled:=False;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Lines.Text:=T.S.Text;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
T.Free;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
T.S.Clear;
Memo1.Clear;
end;
end.
unit Unit2;
interface
uses
Classes, SysUtils, Windows,syncobjs;
type
TTryThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
S:TStringList;
E:TSimpleEvent;
i:Integer;
constructor Create;
destructor Destroy;override;
end;
implementation
procedure TTryThread.Execute;
begin
while not Terminateddo
begin
S.Add(IntToStr(i));
E.WaitFor(1);
Inc(i);
end;
end;
constructor TTryThread.Create;
begin
Inherited Create(True);
S:=TStringList.Create;
E:=TSimpleEvent.Create;
i:=0;
end;
destructor TTryThread.Destroy;
begin
S.Free;
E.Free;
inherited Destroy;
end;
end.