该类TThread
有一个OnTerminate
可用的事件。它由虚拟TThread.DoTerminate()
方法触发,在退出后调用Execute()
,无论是Execute()
正常退出还是通过未捕获的异常退出。如果属性为 True 或属性不为零,我建议覆盖DoTerminate()
并让它触发事件,否则触发事件。OnAbort
Terminated
FatalException
OnReady
更新:假设您正在使用此 TmFileScan 组件,那么我建议您进行以下修改,以便OnReady
始终触发事件:
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;