直接设置一个函数指定到线程的OnTerminate事件
with TDoThread.Create(true)do
begin
OnTerminate := Self.ThreadTerminate;
InterlockedIncrement(FThreadCount);
Resume;
end;
procedure TfrmMain.ThreadTerminate(Sender: TObject);
var
i: Integer;
begin
//线程结束处理事件
FLock.Enter;
try
i := FThreadMagList.IndexOf(Sender);
if i >= 0 then
FThreadMagList.Delete(i);
InterlockedDecrement(FThreadCount);
if FThreadCount = 0 then
begin
btnStart.Enabled := True;
ShowState('服务已停止!');
end;
finally
FLock.Leave;
end;
end;