.NET程序只能启动一个

来源:互联网 发布:玩gta5怎么关闭网络 编辑:程序博客网 时间:2024/04/29 20:29

 

namespace Gosun.PowerAlarm.AppServer.Main{    static class Program    {        /// <summary>        /// 应用程序的主入口点。        /// </summary>        [STAThread]        static void Main()        {            #region 用进程名称保证程序只运行一次            /*****            bool result = false;            Process[] p = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Application.ExecutablePath));            if (p != null && p.Length > 0)                result = true;            if (result == false)            {                Application.EnableVisualStyles();                Application.SetCompatibleTextRenderingDefault(false);                Application.Run(new Server());            }            else            {                MessageBox.Show("该程序已经启动过一次");                Application.Exit();            }             * ******/            #endregion 用进程名称保证程序只运行一次            #region 使用互斥量保证进程唯一            Mutex mutex = new Mutex(false, string.Format("Local//{0}_{1}", Application.StartupPath.Replace('//', '_'), "PowerAlarmAppServerApplication"));            if (!mutex.WaitOne(0, false))            {                mutex.Close();                MessageBox.Show("该程序已经启动过一次");                return;            }            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new Server());                       #endregion 使用互斥量保证进程唯一        }    }}

原创粉丝点击