Reputation: 27296
I need to know, when working with a thread (TThread) in Delphi 7, if I forcefully kill the process, will the thread be terminated
or will it keep on going?
My execute procedure looks like this below, which if the thread is terminated, then this will stop. But what if the thread is never officially terminated?
procedure TJDApplicationThread.Execute;
var
ST: Integer;
begin
ST:= 5;
fStop:= False;
while (not Terminated) and (not fStop) do begin
//----- BEGIN -----
Synchronize(DoSync);
//----- END -----
//Sleep(1000 * ST);
end;
end;
Upvotes: 5
Views: 9258
Reputation: 53870
Setting Terminated doesn't automatically kill the thread.
The Terminated property is set from a different thread to signal to the worker thread that it should terminate. It's then up to the worker thread to obey the signal by checking the Terminated flag in the Execute procedure.
After the Execute procedure is finished, the Thread's Finished property is automatically set.
When the main process is killed, your threads will be interrupted and forcefully killed. If by come to an end, you mean, will it reach the end of the Execute procedure, then no. It could stop right in the middle.
In your main form's close query, it's polite to set the Terminated property on the threads and wait for them to "finish". You can loop through them and check. But after a good timeout, you might want to give up and just close the program, which will interrupt and kill the threads.
Upvotes: 4
Reputation: 219
"Terminate" may (should) also be used in the Windows Shut-down Message process if the user is shutting down the computer and the Thread is running. Terminate should be called at a safe point in your Thread processing. Closing Datasets etc.
Upvotes: 2
Reputation: 8825
Because in user mode, threads cannot exist without a process attached to them, the thread will terminate automatically. However, there may be a delay for process to terminate completely if that thread is doing something that cannot be interrupted immediately (e.g. some I/O operations)
Upvotes: 14