Wating for a window to open to get its handle when launching an RDP file

93 Views Asked by At

I am launching a remote app by using the Process.Start method and passing an .rdp file. This works fine except that this .rdp file causes two windows to open. I need to get a handle on these two windows so that I can set them to be TopMost windows. I need them both to stay in front of the host application.

I beleive I should use FindWindowByCaption(IntPtr.Zero, "targetProcess.exe") but when I call this after Process.Start it fails because the window has not yet opened. How do I wait for this window to open before calling FindWindowByCaption. Here is what I have so far:

                if (gcsMenuItem.ItemUrl.EndsWith(".rdp", StringComparison.InvariantCultureIgnoreCase))
                {                        
                    string fileName = gcsMenuItem.ItemUrl.Split(':')[1];
                    targetProcess = Process.Start(GetProjectDataPath() + fileName);

                    targetProcess.WaitForInputIdle();

                    while (true)
                    {
                        IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, "targetProcess.exe");
                        if (hWnd != IntPtr.Zero)
                        {
                            break;
                        }
                    }
                    targethWnd = targetProcess.MainWindowHandle;
                    

                }

My while loop is no good because it blocks the UI. When the .rdp is launched, the user gets a login window first, then after logging in, it opens two windows. These are the ones I need to set as TopMost.

To set the TopMost, I believe I need to use something like: SetWindowPos(hostWpfWindowHandle, HWND_TOPMOST, x, y, width, height, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOREDRAW);

0

There are 0 best solutions below