Does a thread terminate automatically if its main process is forcefully ended?
Asked Answered
C

3

5

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;
Contribution answered 4/12, 2011 at 5:10 Comment(6)
(I excluded all the irrelevant functionality from the thread execution)Contribution
Question answered below, the main problem is that I'm just scared this thing will get stuck running if Terminated is never set to true.Contribution
When you kill process, everything that process owns, including threads is also killed. The Terminated property is irrelevant. The system just kills everything. No questions asked. Now, the bigger issue is why you are killing processes. It's better to ask them to close.Kappenne
Well I'm no process killer, but sometimes processes are so stubborn I have no choice but to kill them.Contribution
In fact, that also includes using 'Program Reset' in the middle of debugging an application.Contribution
I like @JerryDodgeChlorine
A
14

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)

Antony answered 4/12, 2011 at 5:16 Comment(5)
So no matter what, as long as what happens in that thread comes to an end at one point, the Terminated property will eventually be true, correct?Contribution
Not necessarily... Think about how your code above runs. Your main procedure also runs on a thread and if that thread dies before your child one, then the main thread will not have the time to update that property.Antony
I see, but will the thread eventually come to an end when the process is killed?Contribution
@Jerry, 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.Declarative
What I meant by "there may be a delay for process to terminate completely" is that not all threads will be killed immediately in every circumstances.Antony
D
4

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.

Declarative answered 4/12, 2011 at 5:42 Comment(0)
E
2

"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.

Enterogastrone answered 4/12, 2011 at 16:15 Comment(1)
Good point, that's a fast way to stop a thread upon shutdown/restart.Contribution

© 2022 - 2024 — McMap. All rights reserved.