3

I'm trying to launch an application and bring it to the front. However, the application launches ok, then ends up going behind the launching app. Note that using a similar approach on an already-running minimized application works fine (that code removed from this sample for brevity) - it only fails to work when launching a new instance of the app. Any ideas? Thanks

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Launcher
{
class Program
{
    [DllImport("User32.dll", SetLastError = true)]
    private static extern int SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    private const           int    SW_SHOWMAXIMIZED = 3;

    private static readonly IntPtr HWND_TOP       = new IntPtr(0);
    private const           UInt32 SWP_NOSIZE     = 0x0001;
    private const           UInt32 SWP_NOMOVE     = 0x0002;
    private const           UInt32 SWP_SHOWWINDOW = 0x0040;

    static void Main(string[] args)
    {
            string wd = @"C:\Program Files (x86)\MyFolder";

            string fn = "MyApplication.exe";

            if (!System.IO.File.Exists(wd + @"\" + fn)) return;

            Process p = new Process();
            p.StartInfo.WorkingDirectory = wd;
            p.StartInfo.FileName = fn;

            p.StartInfo.CreateNoWindow = false;
            p.Start(); // app launches OK

            Thread.Sleep(5000);

            SetForegroundWindow(p.MainWindowHandle); // this has no effect
            SetWindowPos(p.MainWindowHandle, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
        }
    }
}
}
radders
  • 923
  • 8
  • 29
  • What kind of application is `MyApplication.exe`? – Erik Philips May 06 '14 at 16:31
  • 1
    There are a whole lot of rules about when `SetForegroundWindow` will work. See the remarks for it's documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539(v=vs.85).aspx – shf301 May 06 '14 at 16:41
  • @Erik: MyApplication.exe is any Windows Forms application. – radders May 07 '14 at 08:01
  • @shf301: I have read all the rules, but am pretty sure my application complies, as it quite happily brings an already-running instance of MyApplication.exe to the foreground. It refuses to do so if it has just launched MyApplication.exe. – radders May 07 '14 at 08:04
  • OK, after reading further questions on here, I managed to resolve the problem by using a combination of `WaitForInputIdle` and a `do` loop which checks for the window caption to be set (which I do in code) to ensure that the app has settled down before invoking the `SetForegroundWindow`. Hopes this helps others – radders May 07 '14 at 08:44
  • Instead of a comment you better self-answer with the complete solution – rene Dec 30 '15 at 10:52

1 Answers1

-1

OK, after reading further questions on here, I managed to resolve the problem by using a combination of WaitForInputIdle and a do loop which checks for the window caption to be set (which I do in code) to ensure that the app has settled down before invoking the SetForegroundWindow. Hopes this helps others

radders
  • 923
  • 8
  • 29