Instead of Sleep(1000)
, use a waitable event.
For example TSimpleEvent
.
FMySimpleEvent.WaitFor(1000);
If you want to exit the thread early, override the TThread.DoTerminate
method and call:
FMySimpleEvent.SetEvent;
Something like this:
procedure Tsrch_slave_thread.Execute;
var
searchactive: integer;
begin
while not terminated do begin
searchactive := master.CMD_LISTCNT
if searchactive = 1 then (master.CMD_LISTCNT's return value = 1, if search finished)
exit;
if (FMySimpleEvent.WaitFor(1000) = wrTimeOut) then
synchronize(procedure begin
with FmasterNode do
text := text + floattostr(Fsw.ElapsedMilliseconds / 1000);
end);
end;
end;
procedure Tsrch_slave_thread.DoTerminate;
begin
Inherited;
FMySimpleEvent.SetEvent;
end;