C#中的Mutex对象认识
我们知道,,有些应用程序可以重复打开,有些只能打开一个,我以前写的程序为了防止用户打开多个程序,都是去遍历Process 查找进程的方式,现在看起来真是不专业,今天看大神的破解分析文章时,认识了mutex,从中get到了新的方法。
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { bool isAppRunning = false; System.Threading.Mutex mutex = new System.Threading.Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out isAppRunning); if (!isAppRunning) { MessageBox.Show("本程序已经在运行了,不要再闹了!"); Environment.Exit(1); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
要注意多用户系统问题,可能Mutex并不那么有效,但应该很少遇到这种情况。
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/67770.html