Get the TThread object for the currently executing thread? Get the TThread object for the currently executing thread? multithreading multithreading

Get the TThread object for the currently executing thread?


From your own answer, it seems maybe you only want to "determine if running in the main thread or not", in which case you can just use

if Windows.GetCurrentThreadId() = System.MainThreadID then// ...

Although this won't work from a DLL created with Delphi if it was loaded by a worker thread.


The latest version of Delphi, Delphi 2009, has a CurrentThread class property on the TThread class.

This will return the proper Delphi thread object if it's a native thread. If the thread is an "alien" thread, i.e. created using some other mechanism or on a callback from a third party thread, then it will create a wrapper thread around the thread handle.


I'm using my own TThread descendent that registers itself in a global list, protected with a lock.

That way, a method in this descendent can walk the list and get a TThread give an ID.