Single Instance Application...


Author
Message
Ivan George Borges
Ivan George Borges
Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
Hi Rogerio.

GetProcessByName is a lot simpler, but has some disavantages:
  • it will allow 2 instances to run if you change the .EXE name
  • if you have 2 separate applications with the same name, it won't allow them to run at the same time
  • Mutex can work with a Global escope, so it will make sure the application run only once in the entire system, even on Terminal Services
  • apparently, GetProcessByName may not access minimized windows properly

Rogerio Mauri
Rogerio Mauri
StrataFrame User (303 reputation)StrataFrame User (303 reputation)StrataFrame User (303 reputation)StrataFrame User (303 reputation)StrataFrame User (303 reputation)StrataFrame User (303 reputation)StrataFrame User (303 reputation)StrataFrame User (303 reputation)StrataFrame User (303 reputation)
Group: StrataFrame Users
Posts: 205, Visits: 629
Hi Ivan...

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //-- Evita mais de uma instância
            bool ok;
            Mutex loMutex = new Mutex(true, Application.ProductName + Application.ProductVersion, out ok);
            if (!ok)
            {
                MessageBox.Show("Aplicativo já está em execução");
                Application.Exit();
                return;
            }

            //-- Enable visual styles on the application
            Application.EnableVisualStyles();


            ...

            //-- Run the application
            StrataFrameApplication.RunApplication();

            //-- Stop the session monitoring before the application exits to remove the low-level event handlers
            SessionLock.StopSessionMonitoring();


            GC.KeepAlive(loMutex);


        }




Rogério Mauri Smile



Ivan George Borges
Ivan George Borges
Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)Strategic Support Team Member (3.5K reputation)
Group: StrataFrame MVPs
Posts: 1.9K, Visits: 21K
Yep, that is it.

I go a bit further on my applications since I have a Licensing control based on licenses per machine. So I create the Mutex under a Global state, this way I can count the number of instances running even on various Terminal Service sessions.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search