禁止多实例运行

       static void Main()
        {
#if false
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
             Application.Run(new Form1());
            //Application.Run(new frmdemo());
#else
            bool ret = false;
            System.Threading.Mutex running = new System.Threading.Mutex(true, Application.ProductName, out ret);
            if (ret)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new Form1());
                Application.Run(new frmdemo());
                running.ReleaseMutex();
            }
            else
            {
                MessageBox.Show("程序已经在运行,请不要同时运行多个本程序!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Application.Exit();
            }
#endif

        }

 

posted @ 2020-10-15 19:26  晨光静默  阅读(123)  评论(0编辑  收藏  举报