The TThread
class has an OnTerminate
event available. It is triggered by the virtual TThread.DoTerminate()
method, which is called after Execute()
exits, regardless of whether Execute()
exits normally or via an uncaught exception. I would suggest overriding DoTerminate()
and have it trigger the OnAbort
event if the Terminated
property is True or the FatalException
property is not nil, and to trigger the OnReady
event otherwise.
Update: Assuming you are using this TmFileScan component, then I suggest you make the following modifications so the OnReady
event is always triggered:
TSearchThread = class(TThread)
private
...
protected
...
procedure DoTerminate; override; // <-- add this
public
destructor Destroy; override; // <-- add this
end;
constructor TSearchThread.Create(Owner: TmFileScan; SubDir, Started: Boolean;
FilePaths, Filter: TStrings; fOnFileFound: TOnFileFoundEvent;
fOnReady: TOnReadyEvent);
begin
inherited Create(true);
...
ffList := TStringList.Create; // <-- add this
...
Resume;
end;
// add this
destructor TSearchThread.Destroy;
begin
ffList.Free;
inherited Destroy;
end;
procedure TSearchThread.Execute;
var
...
begin // function FindFile
// remove this
{
ffList:= TStringList.Create;
try
while not Terminated do
begin
}
for q:= 0 to ffPaths.Count - 1 do
begin
if Terminated then Break; // <-- add this
for n:= 0 to ffFilters.Count - 1 do
begin
if Terminated then Break; // <-- add this
Spec:= ffFilters[n];
RFindFile(BackSlashFix(ffPaths[q]));
end;
end;
// remove this
{
Synchronize(Ready); // <-- remove this
Terminate;
end;
finally
ffList.Free;
end;
}
end;
// add this
procedure TSearchThread.DoTerminate;
begin
Synchronize(Ready);
inherited;
end;