John Thomas
John Thomas

Reputation: 4135

How can I get a list with all the threads created by my application

I want to get a list with all the threads (except the main, GUI thread) from within my application in order to do some action(s) with them. (set priority, kill, pause etc.) How to do that?

Upvotes: 11

Views: 8257

Answers (5)

RRUZ
RRUZ

Reputation: 136391

Another option is use the CreateToolhelp32Snapshot,Thread32First and Thread32Next functions.

See this very simple example (Tested in Delphi 7 and Windows 7).

program ListthreadsofProcess;

{$APPTYPE CONSOLE}

uses
  PsAPI,
  TlHelp32,
  Windows,
  SysUtils;

function GetTthreadsList(PID:Cardinal): Boolean;
var
  SnapProcHandle: THandle;
  NextProc      : Boolean;
  TThreadEntry  : TThreadEntry32;
begin
  SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); //Takes a snapshot of the all threads
  Result := (SnapProcHandle <> INVALID_HANDLE_VALUE);
  if Result then
  try
    TThreadEntry.dwSize := SizeOf(TThreadEntry);
    NextProc := Thread32First(SnapProcHandle, TThreadEntry);//get the first Thread
    while NextProc do
    begin
      if TThreadEntry.th32OwnerProcessID = PID then //Check the owner Pid against the PID requested
      begin
        Writeln('Thread ID      '+inttohex(TThreadEntry.th32ThreadID,8));
        Writeln('base priority  '+inttostr(TThreadEntry.tpBasePri));
        Writeln('');
      end;

      NextProc := Thread32Next(SnapProcHandle, TThreadEntry);//get the Next Thread
    end;
  finally
    CloseHandle(SnapProcHandle);//Close the Handle
  end;
end;

begin
  { TODO -oUser -cConsole Main : Insert code here }
  GettthreadsList(GetCurrentProcessId); //get the PID of the current application
  //GettthreadsList(5928);
  Readln;
end.

Upvotes: 16

vcldeveloper
vcldeveloper

Reputation: 7489

You can use my TProcessInfo class:

var
  CurrentProcess : TProcessItem;
  Thread : TThreadItem;
begin
  CurrentProcess := ProcessInfo1.RunningProcesses.FindByID(GetCurrentProcessId);
  for Thread in CurrentProcess.Threads do
    Memo1.Lines.Add(Thread.ToString);
end;

Upvotes: 6

Darian Miller
Darian Miller

Reputation: 8088

If they are your threads, then I would create an application global Thread Manager to register themselves with upon creation. Then you can properly monitor, pause and shutdown threads gracefully using your Thread Manager.

Upvotes: 0

You can access this information using WMI.
The WIN32_Process can give you all information about process executing on Machine. For each process you can give ThreadsCount, Handle,...

Another class, WIN32_Thread can give you detailled information about all Threads running on Machine. This class hace a property called ProcessId for search especific threads for 1 process (class WIN32_Process).

For test it you can execute this on CommandLine window:

// all processes    
WMIC PROCESS   
// information about Delphi32    
WMIC PROCESS WHERE Name="delphi32.exe"   
// some information about Delphi32    
WMIC PROCESS WHERE Name="delphi32.exe" GET Name,descrption,threadcount,Handle
(NOTE: The handle for delphi32.exe in my machine is **3680**)

Similar you can do with WIN32_Thread using the Handle of process.

Excuse.me for my bad english.

Regards.

Upvotes: 2

Related Questions