windows 如何获取包含我的应用程序创建的所有线程的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2055642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get a list with all the threads created by my application
提问by John Thomas
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?
我想从我的应用程序中获取包含所有线程(主线程、GUI 线程除外)的列表,以便对它们执行一些操作。(设置优先级、终止、暂停等)怎么做?
采纳答案by vcldeveloper
You can use my TProcessInfoclass:
您可以使用我的TProcessInfo类:
var
CurrentProcess : TProcessItem;
Thread : TThreadItem;
begin
CurrentProcess := ProcessInfo1.RunningProcesses.FindByID(GetCurrentProcessId);
for Thread in CurrentProcess.Threads do
Memo1.Lines.Add(Thread.ToString);
end;
回答by RRUZ
Another option is use the CreateToolhelp32Snapshot,Thread32Firstand Thread32Nextfunctions.
另一种选择是使用CreateToolhelp32Snapshot、Thread32First和Thread32Next函数。
See this very simple example (Tested in Delphi 7 and Windows 7).
请参阅这个非常简单的示例(在 Delphi 7 和 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.
回答by K.Sandell
回答by Germán Estévez -Neftalí-
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,...
您可以使用WMI访问此信息。
WIN32_Process 可以为您提供有关在机器上执行的进程的所有信息。对于每个进程,您可以提供 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).
另一个类 WIN32_Thread 可以为您提供有关机器上运行的所有线程的详细信息。此类具有一个名为 ProcessId 的属性,用于搜索 1 个进程的特定线程(类 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.
类似地,您可以使用进程句柄对 WIN32_Thread 进行处理。
Excuse.me for my bad english.
对不起,我的英语不好。
Regards.
问候。
回答by Darian Miller
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.
如果它们是您的线程,那么我将创建一个应用程序全局线程管理器,以便在创建时注册自己。然后,您可以使用线程管理器正常监视、暂停和关闭线程。