unit CtrlThread;
interface
uses
Classes;
type
TCtrlThread =
class( TThread )
public
class procedure KillAllThreads;
constructor Create(CreateSuspended: Boolean);
destructor Destroy;
override;
end;
implementation
uses
Contnrs,
SyncObjs;
var
CtrlThreadList : TObjectList;
CS : TCriticalSection;
{ TCtrlThread }
class procedure TCtrlThread.KillAllCtrlThreads;
var
CTIdx : Integer;
begin
while
( CtrlThreadList.Count > 0 )
do
begin
CS.Enter;
try
for
CTIdx := 0
to
CtrlThreadList.Count - 1
do
begin
TCtrlThread( CtrlThreadList.Items[ CTIdx ] ).FreeOnTerminate := True;
TCtrlThread( CtrlThreadList.Items[ CTIdx ] ).Terminate;
end;
finally
CS.Leave;
end;
end;
end;
constructor TCtrlThread.Create(CreateSuspended: Boolean);
begin
inherited Create( CreateSuspended );
CS.Enter;
try
CtrlThreadList.Add( Self );
finally
CS.Leave;
end;
end;
destructor TCtrlThread.Destroy;
var
CTIdx : Integer;
begin
CS.Enter;
try
CTIdx := CtrlThreadList.IndexOf( Self );
if
( CTIdx >= 0 )
then
CtrlThreadList.Delete( CTIdx );
finally
CS.Leave;
end;
inherited;
end;
initialization
CS := TCriticalSection.Create;
CS.Enter;
try
CtrlThreadList := TObjectList.Create;
CtrlThreadList.OwnsObjects := False;
finally
CS.Leave;
end;
finalization
TCtrlThread.KillAllThreads;
CS.Enter;
try
CtrlThreadList.Free;
finally
CS.Leave;
try
CS.Free;
except
end;
end;
end.