unit Unit1;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.Comctrls, System.ImageList,
Vcl.ImgList,
Vcl.StdCtrls;
type
TmyThread =
class(TThread)
private
fmeineThreadID: cardinal;
public
procedure Execute;
override;
procedure Synthreads;
procedure Syni;
property meineThreadID: Cardinal
Read fmeineThreadID
Write fmeineThreadID;
end;
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
i:integer =0;
var meinThread:
array of TmyThread;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
SetLength(meinthread, 20);
for i := 0
to high(meinThread)
do
meinThread[i]:= TmyThread.Create(false);
end;
{ TmyThread }
procedure TmyThread.Execute;
var z: integer;
begin
fmeineThreadID := ThreadID;
SYNCHRONIZE(Self.Synthreads);
for z := 0
to 50
do
begin
inc(i);
sleep(random(500));
Synchronize(Syni);
if Terminated
then break;
end;
fmeineThreadID:= 0;
Synchronize(Synthreads);
FreeOnTerminate := true;
end;
procedure TmyThread.Syni;
begin
form1.Caption:= inttostr(i);
end;
procedure TmyThread.Synthreads;
var line: integer;
begin
form1.memo1.clear;
for line := Low(meinThread)
to High(meinThread)
do
if meinThread[line].meineThreadID <> 0
then form1.Memo1.Lines.Add(inttostr(meinThread[line].ThreadID));
end;
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
for I := 0
to high(meinThread)
do
if ((meinThread[i] <>
nil )
and (meinThread[i].Terminated = false))
then meinThread[i].Terminate;
end;
end.