在C#中是否可以确定我的进程打开的另一个应用程序中的哪个线程是UI线程?
答案 0 :(得分:8)
@HansPassant已经answered it on MSDN forums:
using System.Diagnostics;
...
public static ProcessThread GetUIThread(Process proc) {
if (proc.MainWindowHandle == null) return null;
int id = GetWindowThreadProcessId(proc.MainWindowHandle, IntPtr.Zero);
foreach (ProcessThread pt in proc.Threads)
if (pt.Id == id) return pt;
return null;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, IntPtr procid);