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
FMainThreadLog: TStrings;
public
constructor Create(Alog: Tstrings; DoOnTerminate: TnotifyEvent);
procedure Execute;
override;
procedure OutputMessage(
const Amessage:
string);
end;
TForm1 =
class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure HandleTerminate(Sender: Tobject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
i:integer =0;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
for i :=0
to 5
do TmyThread.Create(ListBox1.Items, HandleTerminate);
end;
{ TmyThread }
constructor TmyThread.Create(Alog: Tstrings; DoOnTerminate: TnotifyEvent);
begin
inherited Create(false);
FMainThreadLog := Alog;
FreeOnTerminate := true;
OnTerminate := DoOnterminate;
end;
procedure TmyThread.Execute;
var z: integer;
begin
TmyThread.Queue(
nil,
procedure
begin
FMainThreadLog.Add(IntToStr(ThreadID));
end
);
for z := 0
to 49
do
begin
AtomicIncrement(i);
sleep(random(50));
end;
end;
procedure TForm1.HandleTerminate(Sender: Tobject);
begin
Caption := inttostr(i);
end;
procedure TmyThread.OutputMessage(
const Amessage:
string);
begin
if Assigned(FMainThreadLog)
then
FMainThreadLog.Add(Amessage);
end;
end.