C#保证打开的程序是唯一的

 static class Program
        {
            public static System.Threading.Mutex Run;
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                bool noRun = false;
                Run = new System.Threading.Mutex(true, "HumControl", out noRun);
                //检测是否已经运行
                if (noRun)
                {//未运行
                    Run.ReleaseMutex();
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    Application.Run(new FrmLogin());
                }
                else
                {//已经运行
                    //MessageBox.Show("已经有一个实例正在运行!");
                    //切换到已打开的实例
                }
            }

            Program.Run.Close();//关闭现在程序
            Application.Restart(); //重启程序
            Form f = new FrmLogin();
            f.ShowDialog();

 

 

posted @ 2014-05-07 16:40  vaejp  阅读(1007)  评论(0编辑  收藏  举报