@ engkin :
when you call a method of TThread Instance it's executed in the current context NOT in the thread!!!
I agree with that. Where did you see me claiming the opposite?
i repeat once more the TThread Class is just a wapper around OS or lib (dll) function that actually manages the threads on the computer.
I agree with this one as well.
the thread are NOT pascal objects that you can manage ...
Hmmm, it depends on what you mean by "manage".
freeandnil, destroy and free just deallocates the memory in the heap and runs some cleanup in the pascal hierarchy of classes...
Obviously you did not read the source code I quoted above.
FreeAndNil,
Free, and
Destroy do wait for the thread to terminate itself (by exiting its
Execute method) before deallocating the memory in the heap and running cleanup code.
they cannot terminate a running. an OS thread terminates by exiting its main proc.
Hopefully that happens when it checks its
FTerminated which holds the value
True after we call
Terminate,
Free,
FreeAndNil, or
Destroy from another thread.
thread all the methods must be called within the thread it self.. the code that you quoted is just fine if called from the thread it self..
So, you cannot terminate the thread from another thread
Just to be precise, you can call any of these methods from another thread, and yes they do not terminate the thread. They simply indicate that the thread needs to terminate itself by setting FTerminated to true and then they wait for it to happen using
WaitFor.