找主线程TID Delphi版

function GetMainTID(Pid:Cardinal):Cardinal;
{uses Winapi.TlHelp32
function OpenThread(dwDesiredAccess: DWORD; bInheritHandle: BOOL; dwThreadId: DWORD): THandle; stdcall; external Kernel32 name 'OpenThread';
}
var
  MinT:TFileTime;
  hThreadSnap:THandle;
  hNowThread:THandle;
  pe32:TThreadEntry32;
  CreateTime, ExitTime, KernelTime, UserTime: TFileTime;
begin
  Result:=0;
  ZeroMemory(@MinT,SizeOf(TFileTime));
  hThreadSnap:= CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  if(hThreadSnap = INVALID_HANDLE_VALUE) then Exit;//如果获取失败返回
  pe32.dwSize:=SizeOf(TThreadEntry32);
  if Thread32First(hThreadSnap,pe32) then
  begin
    repeat
      if pe32.th32OwnerProcessID = Pid then
      begin
        hNowThread := OpenThread(THREAD_QUERY_INFORMATION, false, pe32.th32ThreadID);
        if GetThreadTimes(hNowThread, CreateTime, ExitTime, KernelTime, UserTime) then
        begin
         // OutputDebugString(PWideChar(Format('%u %u %u',[pe32.th32ThreadID,CreateTime.dwLowDateTime, CreateTime.dwHighDateTime])));
          if Result = 0 then
          begin
            MinT.dwLowDateTime:=CreateTime.dwLowDateTime;
            MinT.dwHighDateTime:=CreateTime.dwHighDateTime;
            Result:= pe32.th32ThreadID;
          end else begin
            if MinT.dwHighDateTime > CreateTime.dwHighDateTime then  //高位早于当前保存的 直接换不用判断低位了
            begin
              MinT.dwLowDateTime:=CreateTime.dwLowDateTime;
              Result:= pe32.th32ThreadID;
            end else if (MinT.dwHighDateTime = CreateTime.dwHighDateTime) and (MinT.dwLowDateTime > CreateTime.dwLowDateTime) then//高位一样对比低位 早于当前保存的就换
            begin
              MinT.dwLowDateTime:=CreateTime.dwLowDateTime;
              Result:= pe32.th32ThreadID;
            end;
          end;
        end;
        CloseHandle(hNowThread);
      end;
    until not Thread32Next(hThreadSnap, pe32);
  end;
  CloseHandle(hThreadSnap);
end;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C++中,可以使用 `<thread>` 头文件中的 `std::this_thread::get_id()` 函数来获取当前线程的ID。而主线程的ID可以通过在程序入口函数 `main()` 中调用该函数来获取。下面是一个示例代码: ``` #include <iostream> #include <thread> int main() { std::cout << "主线程ID:" << std::this_thread::get_id() << std::endl; return 0; } ``` 输出结果类似于: ``` 主线程ID:140621642012288 ``` ### 回答2: 在C语言中打印主线程ID需要使用多线程库,如pthread库。在Linux平台上,可以通过pthread_self()函数获取当前线程线程ID,并通过printf()函数打印输出。下面是一个简单的示例代码: ```c #include <stdio.h> #include <pthread.h> void* thread_func(void* arg) { pthread_t tid = pthread_self(); printf("主线程ID:%ld\n", tid); return NULL; } int main() { pthread_t tid; pthread_create(&tid, NULL, thread_func, NULL); pthread_join(tid, NULL); // 等待线程执行完成 tid = pthread_self(); printf("主线程ID:%ld\n", tid); return 0; } ``` 这里我们创建了一个新的线程thread_func,然后在该线程中通过pthread_self()获取线程ID,并使用printf()函数打印输出。在主线程中,同样也获取主线程ID并打印输出。请注意,pthread_t 是一个整数类型,因此我们使用`%ld`来格式化输出。运行程序后,你将会看到类似如下的输出: ``` 主线程ID:140526538141184 主线程ID:140526538141184 ``` 这就是我们所打印的主线程ID。主线程和新创建的线程具有不同的线程ID,可以通过pthread_self()函数分别获取。 ### 回答3: 在Python中,我们可以使用`threading`模块来打印主线程的ID。该模块提供了用于创建并管理线程的类和函数。 下面是一个示例代码: ```python import threading def print_main_thread_id(): thread_id = threading.get_ident() print("主线程ID:", thread_id) print_main_thread_id() ``` 运行这段代码,我们将会获得类似如下的输出: ``` 主线程ID: 140735213358080 ``` 其中,`threading.get_ident()`函数用于获取当前线程的标识符。在Python中,主线程的标识符通常是一个整数值。在上述示例中,我们通过`print()`函数将获取到的主线程ID打印出来。 需要注意的是,这种方法只适用于在主线程中直接调用的情况。如果在子线程中调用`print_main_thread_id()`函数,将打印出子线程的ID而不是主线程ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值