最安全的线程终止方法:
1.线程建立时用CreateEvent创建一个事件FStopEvent.
FStopEvent := CreateEvent(nil, True, False, nil);
2.用标志boolean FRunning标识线程状态.
3.如果有多个事件要等待,用:
FRunning := true;
while not Terminateddo
begin
case WaitForMultipleObjects(...) of
WAIT_OBJECT_0: break;
end;
end
FRunning := false;
3.如果无事件等待,用
FRunning := true;
while not Terminateddo
begin
if WaitForSingleObject(FStopEvent, 100) = WAIT_OBJECT_0 then
break;
else
...
end
FRunning := false;
4.停止线程时用:
if Suppended then
Resume;
SetEvent( FStopEvent );
Terminated = true;
while FRunning then
Application.MessageProcess();
CloseHandle(FStopEvent);