program project1;
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}cthreads, {$ENDIF}{$ENDIF}
Classes, SysUtils;
type
TLocalThread = class(TThread)
protected
procedure Execute(); override;
public
Action: String;
end;
procedure TLocalThread.Execute();
var
i: Integer = 0;
begin
Sleep(100);
if Action = 'trapped exception' then
begin
Writeln('trapped exception');
try
raise Exception.Create('TLocalThread trapped exception');
except
on E: Exception do
begin
Writeln(E.Message);
end;
end;
end
else if Action = 'untrapped exception' then
begin
Writeln('untrapped exception');
raise Exception.Create('TLocalThread untrapped exception');
end
else if Action = 'hang' then
begin
Writeln('hang');
while not Terminated do //while thread not terminated
begin
Writeln('loop no ', i);
Inc(i);
Sleep(100);
end;
end
else
begin
Writeln('success');
end;
end;
var
tsuccess, ttrappedexception, tuntrappedexception, {%H-}thang: TLocalThread;
begin
{%region 'success'}
Writeln('TESTING success === START');
tsuccess := TLocalThread.Create(True);
try
tsuccess.Action := ''; // success
tsuccess.Start();
//Sleep(3000); //?
finally
tsuccess.WaitFor;
tsuccess.Free();
end;
Writeln('TESTING success === END');
{%endregion}
{%region 'trapped exception'}
Writeln('TESTING trapped exception === START');
ttrappedexception := TLocalThread.Create(True);
try
ttrappedexception.Action := 'trapped exception';
ttrappedexception.Start();
//Sleep(3000); //?
finally
ttrappedexception.WaitFor;
ttrappedexception.Free();
end;
Writeln('TESTING trapped exception === END');
{%endregion}
{%region 'untrapped exception'}
Writeln('TESTING untrapped exception === START');
tuntrappedexception := TLocalThread.Create(True);
try
tuntrappedexception.Action := 'untrapped exception';
tuntrappedexception.Start();
//Sleep(3000); //?
finally
tuntrappedexception.WaitFor;
tuntrappedexception.Free();
end;
Writeln('TESTING untrapped exception === END');
{%endregion}
{%region 'hang'}
Writeln('TESTING hang === START');
thang := TLocalThread.Create(True);
try
thang.Action := 'hang';
thang.Start();
Sleep(3000);
finally
thang.Terminate; //<-- terminate thread
thang.WaitFor;
thang.Free();
end;
Writeln('TESTING hang === END');
{%endregion}
ReadLn;
end.