Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indy worker threads
#1
I'm using TIdHTTPServer and TIdTCPServer components in a Delphi game server app (actually a Windows service app). Does Indy create additional threads beyond what it gives to each individual connection?  If so, is there any way for my app to fetch their ThreadID numbers during runtime?  I ask because I'm presenting a list of active threads and what they are doing. I can of course track the ones I created myself and use the Indy OnConnect/OnDisconnect events to add/remove the connection threads to/from my list. But my list is usually about 16 short of the total that Windows says belong to my app. I'm sure most of those are Windows kernal threads or Delphi worker threads present in all apps but I'd like to narrow down the "unknown" count to as small a number as possible.
Reply
#2
(04-14-2022, 12:22 AM)kbriggs Wrote: Does Indy create additional threads beyond what it gives to each individual connection?

Yes. Each entry in the server's Bindings collection is run in its own thread, listening for incoming connections on its assigned IP/port.

(04-14-2022, 12:22 AM)kbriggs Wrote: If so, is there any way for my app to fetch their ThreadID numbers during runtime?

The listening threads are stored in the server's FListenerThreads member. Indy does not expose direct access to that member, however the member is declared as protected, so you can use an accessor class to reach it, eg:

Code:
type
  TIdHTTPServerAccess = class(TIdHTTPServer)
  end;

var
  List: TIdListenerList;
  I: Integer;
begin
  List := TIdHTTPServerAccess(IdHTTPServer1).FListenerThreads.LockList;
  try
    for I := 0 to List.Count-1 do begin
      // use TIdListenerThread(List[I]).ThreadID as needed...
    end;
  finally
    TIdHTTPServerAccess(IdHTTPServer1).FListenerThreads.UnlockList;
  end;
end;

(04-14-2022, 12:22 AM)kbriggs Wrote: I ask because I'm presenting a list of active threads and what they are doing. I can of course track the ones I created myself and use the Indy OnConnect/OnDisconnect events to add/remove the connection threads to/from my list.

In that case, TIdCustomTCPServer-based servers do have a published OnBeforeListenerRun event that is fired when each listening thread begins running. You can get each ThreadID from in that event. But, there is currently no corresponding OnAfterListenerRun event available when each listening thread stops running. Though, I suppose, in the OnBeforeListenerRun event, you could manually assign your own handler to the TThread.OnTerminated event.

Reply
#3
Thanks, I'll give that a shot.  I've since figured out that I can also use MadExcept (which I'm already using anyway) to grab thread info at runtime, including all the kernel threads.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)