unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure ThreadTerminated(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TMyThread =
class(TThread)
protected
Counter : integer;
// nur für die Ausgabe
procedure Execute;
override;
end;
procedure TMyThread.Execute;
begin
// Der Thread soll einfach nur warten. Dabei wartet er
// zufällig zwischen 1 und 11 Sekunden
Sleep(1000 + random(10001));
end;
procedure TForm1.ThreadTerminated(Sender: TObject);
begin
if Sender
is TMyThread
then
begin
Memo1.Lines.Add('
Der Thread '+IntToStr(TMyThread(Sender).Counter)+'
ist fertig');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
for i:=1
to 6
do
begin
Memo1.Lines.Add('
Erstelle Thread Nummer '+IntToStr(i));
with TMyThread.Create(True)
do
begin
Counter := i;
FreeOnTerminate := True;
OnTerminate := ThreadTerminated;
Resume;
end;
end;
end;
end.