Run one instance from the application

前端 未结 7 961
盖世英雄少女心
盖世英雄少女心 2020-12-16 06:49

I have a windows application (C#) and i need to configure it to run one instance from the application at the time , It means that one user clicked the .exe file and the appl

7条回答
  •  不知归路
    2020-12-16 07:38

    Assuming you are using C#

            static Mutex mx;
            const string singleInstance = @"MU.Mutex";
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main()
            {
                try
                {
                    System.Threading.Mutex.OpenExisting(singleInstance);
                    MessageBox.Show("already exist instance");
                    return;
                }
                catch(WaitHandleCannotBeOpenedException)
                {
                    mx = new System.Threading.Mutex(true, singleInstance);
    
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
    

提交回复
热议问题