unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Contnrs, Dialogs, StdCtrls,unit2;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
OS : Tobjectlist ;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer ;
MyThread : TMyThread ;
begin
if OS = nil then
begin
OS := TObjectList.Create ;
for i:=0 to 10 do
OS.Add(TMyThread.create(false)) ;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i : Integer ;
begin
if OS <>nil then
begin
for i:=0 to 10 do
TerminateThread( (OS.Items as TMythread).Handle,0) ;
OS.Clear ;
FreeAndNil(OS);
end;
end;
end.
unit Unit2;
interface
uses
Classes,SysUtils;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
uses Unit1;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TMyThread }
procedure TMyThread.Execute;
begin
{ Place thread code here }
sleep(1000000);
end;
end.