2013-01-01 16 views
4

C#中可能確定由我的進程打開的另一個應用程序中的哪個線程是UI線程?確定另一個進程中的UI線程

+0

你是如何打開其他應用程序?另外:爲什麼? –

+1

我用「Process.Start」打開它。爲什麼?我需要檢查過程是否卡住(即不處理消息隊列中的消息)。我嘗試了這個過程的「IsResponding」屬性,但它總是「真實的」。 – Idov

+0

是您的源代碼管理下的其他應用程序? – wal

回答

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); 
相關問題