locked
mutex help needed RRS feed

  • Question

  • Hello

                I have two APP1 and  APP2. I want to make sure the APP2 only run when APP1 is not running.

    Please help me to solve the below code

           static Mutex mutex = new Mutex(true, "APP1");
            static Mutex mutexAPP2 = new Mutex(true, "APP2");

            [STAThread]
            static void Main()
            {
                if (mutexAPP2.WaitOne(TimeSpan.Zero, true))
                {

                    if (mutex.WaitOne(TimeSpan.Zero, true))
                    {
                        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
                        Application.Run(new Login());
                    }
                    else
                    {
                        MessageBox.Show("already running.", "Already Running.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show("Already Running", "Already Running.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }



    It's Me

    Wednesday, May 23, 2012 1:21 PM

Answers

  • Should APP1 be able to run even if APP2 is running?  I suspect not.  All you need is a single global mutex that both apps try to create (and take ownership of).  Only one of the apps will succeed in creating it.  If the mutex creation fails (because it already exists) then the second app should just terminate (perhaps with a message).

    Michael Taylor - 5/23/2012
    http://msmvps.com/blogs/p3net

    Wednesday, May 23, 2012 1:51 PM
  • Hello,

    You are already creating 2 global mutex :

           static Mutex mutex = new Mutex(true, "APP1");
            static Mutex mutexAPP2 = new Mutex(true, "APP2");

    According your described needs, a single one sounds enough.


    Kind regards, Sebastien G. - SharpTech

    • Marked as answer by Bob Shen Wednesday, June 6, 2012 9:53 AM
    Thursday, May 24, 2012 6:23 AM

All replies

  • Should APP1 be able to run even if APP2 is running?  I suspect not.  All you need is a single global mutex that both apps try to create (and take ownership of).  Only one of the apps will succeed in creating it.  If the mutex creation fails (because it already exists) then the second app should just terminate (perhaps with a message).

    Michael Taylor - 5/23/2012
    http://msmvps.com/blogs/p3net

    Wednesday, May 23, 2012 1:51 PM
  • Hello

                Many thanks for reply. How can I create single global mutex . Please any example.....

    Note App1 and APP2 are two different exe


    It's Me

    Thursday, May 24, 2012 6:04 AM
  • Hello,

    You are already creating 2 global mutex :

           static Mutex mutex = new Mutex(true, "APP1");
            static Mutex mutexAPP2 = new Mutex(true, "APP2");

    According your described needs, a single one sounds enough.


    Kind regards, Sebastien G. - SharpTech

    • Marked as answer by Bob Shen Wednesday, June 6, 2012 9:53 AM
    Thursday, May 24, 2012 6:23 AM