常用的,獲取進程判斷程序是否已啟動是使用 Process.GetProcessesByName ,以下作一個筆記,記錄一下使用互斥體來判斷。
“OnlyOne”是自定義的系統互斥體的名稱(ID),它的作用域為系統級的,也就是說,其他的程序若也需要禁止重復啟動,此名稱不能重復!
static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { bool bCanRun = false; Mutex mutex = new Mutex(true, "OnlyOne", out bCanRun); if (!bCanRun) { MessageBox.Show("不可重復啟動!"); return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }