C#winform中只允许程序运行一个实例

private static System.Threading.Mutex mutex;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool runNew;
            mutex = new System.Threading.Mutex(true, "myproject", out runNew);
            if (runNew)
            {
                mutex.ReleaseMutex();
               // Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("程序已经在运行。");
            }
        }

posted @ 2012-06-24 13:56  真爱无限  阅读(145)  评论(0编辑  收藏  举报