Jump to content
Darian Miller

Do you name your Threads for debugging?

Recommended Posts

I posted a blog article on this topic yesterday: https://www.ideasawakened.com/post/name-your-threads-even-the-ones-auto-created-by-delphi

 

I'm curious how many others do this sort of thing.  I think it really comes in handy sometimes.

 

I also went looking for the reasons behind each of the Delphi-created threads, other than the main message loop.  Do you have any info on that?  I could swear I read an article on it years ago, but couldn't find it.

  • Like 2

Share this post


Link to post

Darian, I name my threads/tasks with the status also (running, wait, canceled). But this is very slow, very, specially in mobile, I had to put a {$IFDEF USE_THREAD_NAME} to enable only when I need.

Edited by vfbb

Share this post


Link to post

Yes, yes and yes. I have a custom TThread descendant, which sets it's own name to it's classname. Makes debugging a lot more easy than hunting for IDs.

Share this post


Link to post

I wrote my own TThread class descendant which sets the name to the class name.

 

It also automatically sets the name "Main" for the main thread. (Don't the latest Delphi versions already do that?)

Share this post


Link to post

@Darian Miller, Thanks for sharing, but your code will pollute existing thread names such as "main thread" and "thread-created-by-madExcept". I'm not sure if there is a way to check if the thread's named already before changing its name?

Share this post


Link to post
10 hours ago, Jacek Laskowski said:

@Darian Miller Your code starting with initialization is not perfect, because it fires up before all native threads are created.

I imagine it could certainly happen - but I haven't witnessed that I recall in any debugging session using this type of code over quite a few years. 

 

Do you have knowledge of all native threads created and what they are used for?

Share this post


Link to post
8 hours ago, edwinyzh said:

@Darian Miller, Thanks for sharing, but your code will pollute existing thread names such as "main thread" and "thread-created-by-madExcept". I'm not sure if there is a way to check if the thread's named already before changing its name?

 

I don't know of any quick tricks to get the current thread name, perhaps someone will share that if it's available.

 

 

Share this post


Link to post
1 hour ago, Darian Miller said:

I imagine it could certainly happen - but I haven't witnessed that I recall in any debugging session using this type of code over quite a few years. 

 

Do you have knowledge of all native threads created and what they are used for? 

Unfortunatelly I don't.

 

When it comes to the order of starting the "initialization" section it's not always the same, it all depends on the uses section, in my case your code is launched before RTL/VCL threads.
I bypassed this problem by adding a anonymous thread with delay:

procedure NameDelphiThreads(const pMainThreadId : THandle);
begin
  TThread.CreateAnonymousThread(
                                procedure
                                var
                                  vSnapshot:THandle;
                                  vProcessId:THandle;
                                  vTE32:TThreadEntry32;
                                  i:Integer;
                                begin
                                  Sleep(100);
                                  vProcessId := GetCurrentProcessId();
                                  vSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, vProcessId);
                                  if vSnapshot <> INVALID_HANDLE_VALUE then
                                  begin
                                    [...]
                                  end; 
                                end
  ).Start;
end;

 

  • Like 1

Share this post


Link to post
4 minutes ago, Jacek Laskowski said:

When it comes to the order of starting the "initialization" section it's not always the same, it all depends on the uses section, in my case your code is launched before RTL/VCL threads.
I bypassed this problem by adding a anonymous thread with delay:

 

 

That works.  I actually started this code as part of some internal thread management plumbing that I'm building.  I separated it out for the blog article but I used something similar to it in an initialization section for at least a decade prior at a previous job.  I no longer have access to that code to know if it added any delays or other tweaks.  I doubt it did knowing that I wrote it a very long time ago.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×