Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the UI thread in another process

Is it possible in C# to determine which thread in another application that was opened by my process is the UI thread?

like image 860
Idov Avatar asked Feb 18 '23 00:02

Idov


1 Answers

@HansPassant has already 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);
like image 97
Rotem Avatar answered Mar 03 '23 17:03

Rotem