开发者

Running threads count

I'm using delphi 2010, is there anyway to know running threads count of the project via delphi function or windows a开发者_开发问答pi?


you can use the CreateToolhelp32Snapshot function with the TH32CS_SNAPTHREAD flag

see this code.

  uses
  PsAPI,
  TlHelp32,
  Windows,
  SysUtils;

    function GetTThreadsCount(PID:Cardinal): Integer;
    var
        SnapProcHandle: THandle;
        NextProc      : Boolean;
        TThreadEntry  : TThreadEntry32;
        Proceed       : Boolean;
    begin
        Result:=0;
        SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); //Takes a snapshot of the all threads
        Proceed := (SnapProcHandle <> INVALID_HANDLE_VALUE);
        if Proceed 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
              Inc(Result);
              NextProc := Thread32Next(SnapProcHandle, TThreadEntry);//get the Next Thread
            end;
          finally
            CloseHandle(SnapProcHandle);//Close the Handle
          end;
    end;

And call in this way, using the GetCurrentProcessId function wich Retrieves the PID (process identifier) of your application.

Var
Num :integer;
begin
 Num:=GetTThreadsCount(GetCurrentProcessId);
end;


See the sample code here that enumerates the threads of a process.


It's kind of overkill just to get a count, but CreateToolhelp32Snapshot followed by Thread32First and Thread32Next should do the job.


Using WMI you can obtain the process list runninig in System and all information about the process. You must use the Win32_process class.
This class include the method ThreadCount:
···························································
ThreadCount
Number of active threads in a process. An instruction is the basic unit of execution in a processor, and a thread is the object that executes an instruction. Each running process has at least one thread. ···························································

Here (on my web) you can find resources about this theme.

(1) VProcess; Application thah use GLibWMI Library to retrieve all processes running and information about this. ThreadCount is included in this information. The project is free and the source is included (you can see it and evaluate).

Running threads count

(2) You can download GLibWMI and see the component TProcessInfo that give you all information of process (it's used on VProcess). This library it's free and the source code is avaible. You can see it and evaluate the code. See the demos that test this component.

I hope that this are ussefull for you.

Regards.


Using the TH32CS_SNAPPROCESS flag seems to be more efficient (5 times faster according to my tests).

function GetProcessThreadCount(ProcessId: Cardinal): Integer;
var
  SnapshotHandle: THandle;
  ProcessEntry32: TProcessEntry32;
  CanContinue: Boolean;
begin
  Result := 0;
  SnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if SnapshotHandle <> INVALID_HANDLE_VALUE then
    try
      ProcessEntry32.dwSize := SizeOf(ProcessEntry32);
      CanContinue := Process32First(SnapshotHandle, ProcessEntry32);
      while CanContinue do
      begin
        if ProcessEntry32.th32ProcessID = ProcessId then
        begin
          Result := ProcessEntry32.cntThreads;
          Exit;
        end;
        CanContinue := Process32Next(SnapshotHandle, ProcessEntry32);
      end;
    finally
      CloseHandle(SnapshotHandle);
    end;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜