如何获取包含我的应用程序创建的所有线程的列表

发布于 2024-08-17 12:14:02 字数 74 浏览 2 评论 0原文

我想从我的应用程序中获取包含所有线程(除了主 GUI 线程)的列表,以便对它们执行一些操作。 (设置优先级、终止、暂停等) 怎么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

本宫微胖 2024-08-24 12:14:02

另一种选择是使用 CreateToolhelp32Snapshot,< a href="http://msdn.microsoft.com/en-us/library/ms686728%28VS.85%29.aspx" rel="nofollow noreferrer">Thread32First 和 Thread32Next 函数。

请参阅这个非常简单的示例(在 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.

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.
简美 2024-08-24 12:14:02

您可以使用我的 TProcessInfo 类:

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

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;
似最初 2024-08-24 12:14:02

您可以使用 WMI 访问此信息。
WIN32_Process 可以为您提供有关机器上执行的进程的所有信息。对于每个进程,您可以提供 ThreadsCount、Handle...

另一个类,WIN32_Thread 可以为您提供有关机器上运行的所有线程的详细信息。该类有一个名为 ProcessId 的属性,用于搜索 1 个进程的特定线程(WIN32_Process 类)。

为了测试它,您可以在命令行窗口上执行此操作:

// 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**)

类似,您可以使用进程句柄对 WIN32_Thread 执行此操作。

请原谅我的英语不好。

问候。

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.

谜兔 2024-08-24 12:14:02

如果它们是您的线程,那么我将创建一个应用程序全局线程管理器以在创建时注册它们自己。然后,您可以使用线程管理器正确地监视、暂停和关闭线程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文