0

I've written a simple windows app with one form. It's purpose is to run only in one instance. I used the mutex approach and throw a message when a second instance tries to run. Now I would like to change this message box and I only want to bring the first Instance to the top when trying to fire a second instance. My code is currently:

namespace WindowsFormsApplication2
{
    static class Program
    {

        [STAThread]                
        static void Main()
        {


            bool mutexCreated = false;
            System.Threading.Mutex mutex = new System.Threading.Mutex(true,@"Local\WindowsFormsApplication2.WindowsFormsApplication2.exe", out mutexCreated);

           if(!mutexCreated )  
           {
               if( MessageBox.Show("The application is already running.Hit the OK to exit",             "",MessageBoxButtons.OK, MessageBoxIcon.Information ) != DialogResult.Cancel )
               {
                  mutex.Close(); 
                  return;
               }
           }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
roy
  • 61
  • 1
  • 1
  • 3
  • Can you try just to activate Form1? – Jester Jan 14 '13 at 18:57
  • hey jester. the point behind this move is that the application can run only in one instance but whenever the user tries to fire another instance the first instance form jumps to the top and the second one is cancelled. e.g - notepad++ works like that, when trying to open it twice, instead of opening another window the preopened window pops to the top. – roy Jan 14 '13 at 19:08

2 Answers2

2

.NET already has very good support for this, supporting both single instance apps and letting the first app know about another instance getting started. Strongly favor this over spinning your own. Use the WindowsFormsApplicationBase class, alter the Program.cs file like this:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;    // Add reference to Microsoft.VisualBasic

namespace WindowsFormsApplication1 {
    class Program : WindowsFormsApplicationBase {
        public Program() {
            this.EnableVisualStyles = true;
            this.IsSingleInstance = true;
            this.MainForm = new Form1();
        }
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e) {
            e.BringToForeground = true;
        }
        [STAThread]
        public static void Main(string[] args) {
            new Program().Run(args);
        }
    }
}

Note how the OnStartupNextInstance() method ensures that the main window is brought back into the foreground. You can also use its e.CommandLine property to get command line arguments passed from the second instance to the main instance. Which tends to be useful if you use a file association.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

This is the most recent version of a post that does the same thing I can find. Essentially, you grab the other process info and then bring it forward.

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/42b3db75-e61e-4f59-bf2b-c96a40cfb4e4

Brian P
  • 1,569
  • 13
  • 23
  • i actually want to replace this chunk : if(!mutexCreated ) { if( MessageBox.Show("The application is already running.Hit the OK to exit", "",MessageBoxButtons.OK, MessageBoxIcon.Information ) != DialogResult.Cancel ) { mutex.Close(); return; with a code that will bring the first instance to top instead of opening a new one. – roy Jan 14 '13 at 19:43
  • Exactly, so use the link attached to bring focus to an already running process. Since this will be a second instance, it will be another process. – Brian P Jan 14 '13 at 20:30