Check if instance is running on open and if minimized to system tray bring to front (unhide it)

After reading some of the solutions on how to verify if an instance of the same program is already running, and if running bring it to the front (and restore if minimized) this is the solution I'm using:

[DllImport("user32.dll")] private static extern 
    bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern 
    bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern 
    bool IsIconic(IntPtr hWnd);

private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;

static void Main()
    // get the name of our process
    string proc=Process.GetCurrentProcess().ProcessName;
    // get the list of all processes by that name
    Process[] processes=Process.GetProcessesByName(proc);
    // if there is more than one process...
    if (processes.Length > 1)
        // Assume there is our process, which we will terminate, 
        // and the other process, which we want to bring to the 
        // foreground. This assumes there are only two processes 
        // in the processes array, and we need to find out which 
        // one is NOT us.

        // get our process
        Process p=Process.GetCurrentProcess();
        int n=0;        // assume the other process is at index 0
        // if this process id is OUR process ID...
        if (processes[0].Id==p.Id)
            // then the other process is at index 1
        // get the window handle
        IntPtr hWnd=processes[n].MainWindowHandle;
        // if iconic, we need to restore the window
        if (IsIconic(hWnd))
            ShowWindowAsync(hWnd, SW_RESTORE);
        // bring it to the foreground
        // exit our process
    // ... continue with your application initialization here.

My problem is that the program I've written I have it so it minimizes to the system try by hiding the form, in this manner:

private void Form1_Resize(object sender, EventArgs e)
        if (WindowState == FormWindowState.Minimized)

And I use a notifyIcon. So as you can see, and from what I can tell is happening, when I start the second instance which will check if there's an instance already running, it doesn't seem capable of "unhiding" the form that's minimized to the system tray of the first instance of the program. So wondering if there was a way to do it.

If I remove the code of hiding the form so it minimizes normally, the verification and bringing the window to the front works as I expected, so I figure the problem is to find a way to unhide the form instead.


Read more here:

Content Attribution

This content was originally published by MasterOfNothing at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: