2010-01-13 89 views

回答

4

你可以用我TProcessInfo類:

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

您需要一個自定義庫,名爲** Process Info ** – TPAKTOPA 2013-12-17 14:02:37

+0

鏈接已損壞,該域名不再存在。 2012年的網頁有一個[存檔副本](http://web.archive.org/web/20121010001118/http://vcldeveloper.com/products/products-components/process-info),但代碼下載不見了。 – 2016-07-27 23:30:45

1

您可以訪問使用WMI此信息。
WIN32_Process可以爲您提供有關在Machine上執行的進程的所有信息。對於每個進程,你可以給ThreadsCount,Handle,...

另一個類,WIN32_Thread可以給你有關Machine上運行的所有線程的詳細信息。此類具有一個名爲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做。

Excuse.me for my bad english。

問候。

+0

非常簡單實用的例子,謝謝! – TPAKTOPA 2013-12-17 15:04:29

12

另一種選擇是使用CreateToolhelp32SnapshotThread32FirstThread32Next功能。

看到這個非常簡單的例子(在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. 
+0

不要忘記TThreadEntry.dwSize是由Thread32First和Thread32Next設置的**,所以你應該在訪問其他字段前檢查它。不同版本的Windows返回不同數量的有效數據。 – 2014-04-02 17:22:05

0

如果它們是你的線程,那麼我將創建一個應用程序全局線程管理器來創建時自行註冊。然後,您可以使用線程管理器正常監視,暫停和關閉線程。

相關問題