Saturday, 22 March 2014

run only one instance exe c#

Getting a process Run by name and deciding by name only is bad practice, it should be very good if we will give some instance to run the application
My mutex names are usually GUID's to ensure they don't accidentally collide with other applications.
namespace SystemTrayApp
{
       ///
       ///
       ///
       static class Program
       {
        private const string AppMutexName = "47A12CEB-F6BE-4a4b-915E-DB85D5929555";
              ///
              /// The main entry point for the application.
              ///
              [STAThread]
              static void Main()
              {
            bool mutexCreated = false;
            Mutex appMutex = new Mutex(false, AppMutexName, out mutexCreated);
            if (!appMutex.WaitOne(0))
            {
                MessageBox.Show("Only one application at a time!!");
                return;
            }
                     Application.EnableVisualStyles();
                     Application.SetCompatibleTextRenderingDefault(false);
                     // Show the system tray icon.                                
                     using (ProcessIcon pi = new ProcessIcon())
                     {
                           pi.Display();
                           // Make sure the application runs!
                           Application.Run(new DetectActive());
              //  new DetectActive().WindowState = FormWindowState.Minimized;
                ContextMenus.isExit = 0;
                     }
              }
       }
}

No comments:

Post a Comment

Others

You will like to read it