C prohibits instances where an application is started multiple times

  • 2020-05-17 06:17:49
  • OfStack

Usually, we double-click an exe file to run an entity of the program, double-click the exe file once, and then run another entity of the application. In the case of QQ games, one computer can only run one QQ gaming hall (although I've heard of double-open plug-ins before).

Can our program disable multiple booting like QQ? The answer is yes. Here is a simple implementation: Mutex(mutual exclusion).

Mutex(mutual exclusion, mutual exclusion) is a class in.Net Framework that provides synchronous access across multiple threads. It is very similar to the Monitor class in that they each have only one thread that can hold a lock. While the operating system can recognize the mutex with a name, we can give the mutex a unique name and add one such mutex before the program starts. This checks the existence of the named mutex each time the program starts. If it exists, the application exits.


static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createdNew;
            // The system recognizes named mutexes, so it can be used to disable the application from starting twice 
            // The first 2 Three parameters can be set to the name of the product :Application.ProductName
            // Each time the application is started, the name is validated as SingletonWinAppMutex Is the mutual exclusion of 
            Mutex mutex = new Mutex(false, "SingletonWinAppMutex", out createdNew);

            // If run, it is displayed at the front end 
            //createdNew == false , indicating that the program has run 
            if (!createdNew)
            {
                Process instance = GetExistProcess();
                if (instance != null)
                {
                    SetForegroud(instance);
                    Application.Exit();
                    return;
                }
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
        /// <summary>
        ///  Check to see if the program is running 
        /// </summary>
        /// <returns></returns>
        private static Process GetExistProcess()
        {
            Process currentProcess = Process.GetCurrentProcess();
            foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
            {
                if ((process.Id != currentProcess.Id) && 
                    (Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
                {
                    return process;
                }
            }
            return null;
        }
        /// <summary>
        ///  Make the program front end display 
        /// </summary>
        /// <param name="instance"></param>
        private static void SetForegroud(Process instance)
        {
            IntPtr mainFormHandle = instance.MainWindowHandle;
            if (mainFormHandle != IntPtr.Zero)
            {
                ShowWindowAsync(mainFormHandle, 1);
                SetForegroundWindow(mainFormHandle);
            }
        }
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
    }


Related articles: