0

I have an application that I want to Hide instead of closing in order to run in the background. If I hide it, when I relaunch the application, how do I kill the new instance while Unhiding/reactivate the currently running application? I can terminate the current instance with;

        string currPrsName = Process.GetCurrentProcess().ProcessName;
        //Get the name of all processes having the same name as this process name 
        Process[] theProcessWithThisName = Process.GetProcessesByName(currPrsName);
        if (theProcessWithThisName.Length > 1)
        {
            string message = "'" + currPrsName + ".exe'" + " is already running.\nOperation canceled";
            string caption = "ALREADY RUNNING";
            MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Environment.Exit(0);    //Terminate this instance
        }

I have tried various ways to unhide the app with no success.

reg001
  • 11
  • 2
  • 2
    You would probably need some form of IPC to do this, unless you're just minimising the window (or sending it to the tray). Or, you could consider sending `SW_HIDE` and `SW_SHOW` using [ShowWindow(..)](http://msdn.microsoft.com/en-gb/library/windows/desktop/ms633548(v=vs.85).aspx), if you're comfortable using the WinApi – Rudi Visser Jan 20 '13 at 22:27
  • Use the built-in support in the .NET framework: http://stackoverflow.com/questions/14324676/how-to-bring-the-main-form-to-top-when-trying-to-run-a-second-instance-of-the-ap/14326291#14326291 – Hans Passant Jan 21 '13 at 00:29

1 Answers1

0

There's a nice walkthrough which, I think, answers all of your questions. Take a look: http://www.codeproject.com/Articles/32908/C-Single-Instance-App-With-the-Ability-To-Restore

Haspemulator
  • 11,050
  • 9
  • 49
  • 76