unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyThread =
class(TThread)
Private
FNum: Integer;
procedure Execute;
override;
procedure IncNum;
public
end;
TForm1 =
class(TForm)
lbl2: TLabel;
lbl1: TLabel;
btn2: TButton;
btn1: TButton;
procedure btn2Click(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure CreateMyThread;
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
MyThread: TMyThread;
implementation
{$R *.dfm}
procedure TForm1.btn2Click(Sender: TObject);
begin
if lbl2.Caption = '
Eins'
then
begin
lbl2.Caption := '
Zwei';
end
else
begin
lbl2.Caption := '
Eins';
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
if btn1.Caption = '
Start'
then
begin
btn1.Caption := '
Stop';
CreateMyThread;
MyThread.Start;
end
else
begin
btn1.Caption := '
Start';
MyThread.Terminate;
MyThread :=
nil;
end;
end;
procedure TForm1.CreateMyThread;
begin
if not Assigned(MyThread)
then
begin
MyThread := TMyThread.Create(true);
MyThread.FreeOnTerminate := true;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateMyThread;
end;
procedure TMyThread.IncNum;
begin
FNum := StrToInt(Form1.lbl1.Caption);
Inc(FNum, 1);
Form1.lbl1.Caption := IntToStr(FNum);
end;
procedure TMyThread.Execute;
begin
while (
not terminated)
do
begin
Synchronize(IncNum);
Sleep(800);
end;
end;
end.