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

标签 windows delphi multithreading process

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

最佳答案

另一种选择是使用 CreateToolhelp32Snapshot , Thread32FirstThread32Next功能。

查看这个非常简单的示例(已在 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.

关于windows - 如何获得包含我的应用程序创建的所有线程的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2055642/

相关文章:

c# - 在新线程中运行函数 C# Windows 应用程序

PHP + PThreads + Redis/Predis = zend_mm_heap 已损坏?

c++ - Windows 7 shell 函数的链接问题

ios - Firemonkey iOS TExpanderButton onclick

delphi - 如何使用Delphi在控制台应用程序中显示进度条?

delphi - 如何在 Delphi IDE 中更快地创建自动属性?

java - 不正确构造的对象是否只影响它在构造函数内发布的线程的可见性?

windows - MSYS : How to convert backslash to forward slash? 上的 TortoiseSVN 命令行输出不方便

windows - docker:是否可以从另一个容器中启动 native Windows 同级容器?

php - 在 PHP 中使用 exec 命令时可能出现的权限问题