导航

(转)C# 单实例

Posted on 2009-03-13 12:02  Heclei  阅读(294)  评论(0编辑  收藏  举报

 static private bool IsOneInstance()
        {
#if !DEBUG
            bool one;
            mutex = new System.Threading.Mutex(true, "VIPSHOP", out one);
            return one;
#else
            return true;
#endif
        }

 

            if (IsOneInstance())
            {
                Application.Run(new LoginFrm());
            }
            else
            {
                MessageBox.Show("只允许运行一个实例", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

 

在Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

释放mutex

if (mutex != null)
            {
                mutex.ReleaseMutex();
                mutex.Close();
            }