Does a thread automatically terminate if its main process is forcibly terminated?

I need to know when working with a thread (TThread) in Delphi 7, if I forcefully kill a process, will the thread be terminated or will it continue to work?

My execution routine looks like the one shown below, which, if the thread is completed, it will stop. But what if a thread never officially stops?

 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; 
+4
source share
3 answers

Since in user mode, threads cannot exist without a process attached to them, the thread will automatically terminate. However, there may be a delay to complete the process if this thread does something that cannot be immediately interrupted (for example, some I / O)

+11
source

Setting Complete does not automatically kill the stream.

The specified property is set from another thread to signal this worker thread, which it must complete. Then, to the workflow, to obey the signal by checking the "Finish" flag in the "Run" procedure.

After the Execute procedure completes, the Thread Finished property is automatically set.

When the main process is killed, your threads will be interrupted and will be forcibly killed. If you come to an end, you mean whether it reaches the end of the Execute procedure, and then not. He can stop right in the middle.

In your main form, close the request, politely set the Terminated property on the threads and wait for them to finish. You can go through them and check. But after a good timeout, you can refuse and just close the program, which will interrupt and kill threads.

+4
source

Shutdown can (should) also be used in the process of shutting down Windows if the user shuts down the computer and the thread is running. Terminate should be called in a safe place in your Thread handling. Closing datasets, etc.

+2
source

Source: https://habr.com/ru/post/1384537/


All Articles