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)
procedure Execute;
override;
procedure Synthreads;
procedure Syni;
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 threads:
array of cardinal;
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;
var cnt: integer;
begin
inherited;
SetLength(threads, length(threads)+1);
threads[(high(threads))] := self.ThreadID;
SYNCHRONIZE(Self.Synthreads);
for z := 0
to 50
do
begin
inc(i);
sleep(random(500));
Synchronize(Syni);
if Terminated
then break;
end;
for cnt:= low(threads)
to High(threads)
do
if threads[cnt] = self.ThreadID
then
begin
threads[cnt] := threads[high(threads)];
SetLength(threads, length(threads)-1);
end;
Synchronize(Synthreads);
end;
procedure TmyThread.Syni;
begin
form1.Caption:= inttostr(i);
end;
procedure TmyThread.Synthreads;
var line: integer;
begin
form1.memo1.clear;
for line := Low(threads)
to High(threads)
do
form1.Memo1.Lines.Add(inttostr(threads[line]));
end;
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
if Length(meinThread) >0
then
for I := 0
to high(meinThread)
do
if ((meinThread[i] <>
nil )
and (meinThread[i].Terminated = false))
then meinThread[i].Terminate;
end;
end.