c# 怎样获得想要的线程ID

不是当前的大侠们能指出相应的参数吗 在下感激不尽
2025-05-20 22:26:19
推荐回答(5个)
回答1:

GetThreadId 根据线程句柄得到线程ID。

GetWindowThreadProcessId ,根据窗口句柄得到此窗口所在线程的ID(也同时得到进程的ID)

OpenThread,能根据ID得到线程的句柄

回答2:

这个问题给100分。。。你很大方啊。
GetThreadId 根据线程句柄得到线程ID。

GetWindowThreadProcessId ,根据窗口句柄得到此窗口所在线程的ID(也同时得到进程的ID)

OpenThread,能根据ID得到线程的句柄

获取在关联进程中运行的一组线程:Process.Threads 属性
获取线程的唯一标识符:ProcessThread.Id

回答3:

获取在关联进程中运行的一组线程:Process.Threads 属性
获取线程的唯一标识符:ProcessThread.Id

回答4:

必须借助于系统---下面是windows操作系统---例如你要将启动的一个Excel进程中的某个线程获取到,并且关闭,要求此时不影响其他使用。可以操作如下:
[System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private void Kill(Microsoft.Office.Interop.Excel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd); //得到这个句柄,具体作用是得到这块内存入口

int k = 0;
GetWindowThreadProcessId(t, out k); //得到唯一标志k
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); //k的引用
p.Kill(); //关闭k
}

回答5:

System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
bool foundProcess = false;
System.Diagnostics.ProcessThreadCollection threads = null;
System.Threading.Thread thread = null;
foreach (System.Diagnostics.Process p in processes)
{
if (p.ProcessName == "notepad")
{
threads = p.Threads;
foundProcess = true;
break;
}
}
if (foundProcess)
{
foreach (System.Threading.Thread th in threads)
{
if (th.Name == "xxxxx")
{
thread = th;
break;
}
}
}

if (thread != null)
{
// 不知道threadId是不是你说的线程ID.
int threadId = thread.ManagedThreadId;
}