WindowsForm只允许运行一个实例

时间:2020-12-30 19:48:22
static class Program
    {
        private static Mutex onlyOne;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            onlyOne = new Mutex(true, Process.GetCurrentProcess().ProcessName);
            if (onlyOne.WaitOne(0, false))
            {
                Application.Run(new frmRelease());
            }
            else
            {
                MessageBox.Show("应用程序已启动!");
            }
        }
    }