运行线程数 [英] Running threads count
本文介绍了运行线程数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
我正在使用delphi 2010,有没有通过delphi函数或windows api知道运行的线程计数?
您可以使用 CreateToolhelp32Snapshot
功能与 TH32CS_SNAPTHREAD
标志
查看此代码
使用
PsAPI,
TlHelp32,
Windows,
SysUtils;
函数GetTThreadsCount(PID:Cardinal):整数;
var
SnapProcHandle:THandle;
NextProc:Boolean;
TThreadEntry:TThreadEntry32;
继续:布尔;
begin
结果:= 0;
SnapProcHandle:= CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0); //获取所有线程的快照
继续:=(SnapProcHandle<> INVALID_HANDLE_VALUE);
如果继续,然后
尝试
TThreadEntry.dwSize:= SizeOf(TThreadEntry);
NextProc:= Thread32First(SnapProcHandle,TThreadEntry); //获取第一个线程
而NextProc do
begin
如果TThreadEntry.th32OwnerProcessID = PID然后//检查所有者Pid对PID请求
Inc(Result);
NextProc:= Thread32Next(SnapProcHandle,TThreadEntry); //获取下一个线程
end;
finally
CloseHandle(SnapProcHandle); //关闭句柄
end;
结束
以这种方式调用,使用 GetCurrentProcessId
函数检索PID(进程标识符)你的应用程序。
Var
Num:integer;
begin
Num:= GetTThreadsCount(GetCurrentProcessId);
结束
I'm using delphi 2010, is there anyway to know running threads count of the project via delphi function or windows api?
解决方案
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;
这篇关于运行线程数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文