0

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);

Ray
  • 4,019
  • 7
  • 33
  • 82
  • You could try putting a sleep for 100ms within your wait loop, to reduce cpu drain. MS docs say you have to do `targetProcess.Refresh` before using `targetProcess.MainWindowHandle`. I would think it is better to get the `targetProcess.MainWindowHandle' within your while loop. Try some of those. – tgolisch Jan 26 '21 at 16:36
  • When it reaches the line targetProcess.MainWindowHandle, it gives an InvalidOperationException: "Process has exited, so the requested information is not available." This is strange because the windows are opened when this line is executed. – Ray Jan 26 '21 at 16:54
  • You'll need a background thread to prevent the UI from blocking. Why do you think your window caption is "targetprocess.exe"? That doesn't seem right? – NetMage Jan 26 '21 at 22:38
  • @NetMage Do you mean a background thread that runs the while loop? targetprocess.exe was just an example. – Ray Jan 27 '21 at 23:12

0 Answers0