WinForm 实现exe单例模式

        static void Main()
        {
            Application.EnableVisualStyles();                       //样式设置
            Application.SetCompatibleTextRenderingDefault(false);   //样式设置

            //获取当前进程名称
            string currentProcessName = Process.GetCurrentProcess().ProcessName;

            //把该名称的所有进程的列表
            Process[] process = Process.GetProcessesByName(currentProcessName);

            if (process.Length > 1)
            {
                MessageBox.Show("程序已经运行!", " 提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            FormSplash sp = new FormSplash();                               //启动窗体
            sp.pictureBox1.SendToBack();
            sp.Show();                                              //显示启动窗体
            context = new ApplicationContext();
            context.Tag = sp;
            Application.Idle += new EventHandler(Application_Idle); //注册程序运行空闲去执行主程序窗体相应初始化代码
            Application.Run(context);

        }

猜你喜欢

转载自blog.csdn.net/qq_14874791/article/details/118682033