55

We only want one instance of our app running at any one time. So on start up it looks to see if the app is running and if it is, it calls SetForegroundWindow on the Main Window.

This is all good and well ... for the most part..

When our app starts up it will show a Splash screen and a Logon form. Both of these forms have ShowInTaskBar = false.

Because of this, if you try to start up another copy of the app when the Logon form is showing, that Logon form is not brought to the front!

Especially as the user cant see anything in the taskbar as well, all they figure is that the app is duff and cannot start. There is no indication that there is another instance running.

Is there any way around this problem?

1
  • Question: does setting the Owner property of the Logon form not solve this or does it have no effect?
    – Asher
    Apr 14, 2010 at 12:38

3 Answers 3

66

Well, code is here. Even if the ShowInTaskBar is false, you should be able to bring it to the front.

    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    public static void bringToFront(string title) {
        // Get a handle to the Calculator application.
        IntPtr handle = FindWindow(null, title);

        // Verify that Calculator is a running process.
        if (handle == IntPtr.Zero) {
            return;
        }

        // Make Calculator the foreground application
        SetForegroundWindow(handle);
    }

Note: you should FindWindow using the form's class and not by name as the splash screen forms sometimes do not have titles or even the controlbox. Use Spy++ to dig deeper.

Use FindWindow on splash. I think this is what you want to do - bring the splash screen in front while loading of the main form.

3
  • 2
    I was doing a System.Diagnostics.Process.GetProcessesByName call to get the process and then calling SetForegroundWindow on the process.MainWindowHandle. Without ShowInTaskbar, there was no MainWindowHandle. FindWindow is a good way to work around this. Thanks! Apr 14, 2010 at 11:37
  • So many ways to do this yet this was the only one that worked for me!
    – Lee
    Aug 13, 2016 at 23:02
  • Not working under Windows 10 if calculator is minimized.
    – Pollitzer
    Jun 19, 2020 at 8:56
54

I think this is the better solution because its restores from minimized state:

public static class WindowHelper
{
    public static void BringProcessToFront(Process process)
    {
        IntPtr handle = process.MainWindowHandle;
        if (IsIconic(handle))
        {
            ShowWindow(handle, SW_RESTORE);
        }

        SetForegroundWindow(handle);
    }

    const int SW_RESTORE = 9;

    [System.Runtime.InteropServices.DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(IntPtr handle);
    [System.Runtime.InteropServices.DllImport("User32.dll")]
    private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
    [System.Runtime.InteropServices.DllImport("User32.dll")]
    private static extern bool IsIconic(IntPtr handle);
}

Simple call:

WindowHelper.BringProcessToFront(process);
5
  • 4
    Upvoting this answer since it restores from a minimized state as well.
    – oppassum
    Dec 27, 2016 at 19:24
  • Just want to add to answer, that it won't work in debug mode - the process will be set active for a moment and then hided again. But it works fine if you just run your app.
    – Vladimir
    Jul 10, 2018 at 14:07
  • 1
    I upvoted this solution as it mentions how to do that with a process handle. Additionally, I like the completeness of the answer including pinvoke stuff. Aug 24, 2018 at 18:22
  • Here's an even better solution than the better solution: stackoverflow.com/questions/46974192/…
    – Kim Homann
    May 28, 2020 at 12:10
  • 1
    Not working under Windows 10 if calculator is minimized.
    – Pollitzer
    Jun 19, 2020 at 9:37
1
FindWindow(null, title);

Will find the first window that matches the query. This may lead to unexpected behavior if another window uses the same title.

Although the chances of this to happen may seem rare or impossible (single instance application) this can easily occur. The windows explorer for instance uses the name of the selected directory as window title (although invisible). Now if the window title is a common term or matches the name of the application directory this can be an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.