网站首页 > 网络编程教程 > C#/CSHARP教程 > C# 重启互斥程序的一个方法

C# 重启互斥程序的一个方法

  • 作者:互联网
  • 时间:2009-12-04 16:53:20

在程序运行结束时释放互斥对象,然后执行重启。

        static string mutexFlagStr = "TEST1";
        static public bool requestRestart = false;
        static void Main()
        {

            Ap***cation.EnableVisualStyles();
            Ap***cation.SetCompatibleTextRenderingDefault(false);
            // 通过互斥来防止程序多重启动
            bool blnCreate;
            Mutex m = new Mutex(true, mutexFlagStr, out   blnCreate);
            if (blnCreate)
            {
                Ap***cation.Run(new FormMain());
                m.***easeMutex();
                m.Close();
                if (requestRestart)
                {
                    Ap***cation.Restart();
                }
            }
            else
            {
                Me***geBox.Show("程序已经启动,不能再次启动!");
                Ap***cation.Exit();
            }

        }

在执行重启时,先将requestRestart 赋值为true,然后执行Ap***cation.Exit();
-