互斥進程(程式), 簡單點說,就是在系統中只能有該程式的一個執行個體運行. 現在很多軟體都有這功能,如Maxthon 可以設定為"只允許開啟一個表單",還有Bitcomet等. 我也是看到這些軟體的這個功能才來研究這個問題的. 要實現程式的互斥,通常有三中方式,下面用 C# 語言來實現: 實現方式一: 使用線程互斥變數. 通過定義互斥變數來判斷是否已運行執行個體.C#實現如下: 把program.cs檔案裡的Main()函數改為如下代碼: static void Main() { bool runone; System.Threading.Mutex run = new System.Threading.Mutex(true, "xinbiao_a_test", out runone); if (runone) { run.ReleaseMutex(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else { MessageBox.Show("已經運行了一個執行個體了。"); } } 說明:程式中通過語句 System.Threading.Mutex run = new System.Threading.Mutex(true, "xinbiao_a_test", out runone); 來申明一個互斥體變數run,其中"xinbiao_a_test"為互斥體名,布爾變數runone用來儲存是否已經運行了該程式案例.實現方式二: 採用判斷進程的方式,我們在運行程式前,尋找進程中是否有同名的進程,同時運行位置也相同程,如是沒有運行該程式,如果有就就不運行.在C#中應用System.Diagnostics名字空間中的Process類來實現,主要代碼如下: 1,在program.cs檔案中添加函數如下: public static System.Diagnostics.Process RunningInstance() { System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess(); System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses(); foreach (System.Diagnostics.Process process in processes) //尋找相同名稱的進程 { if (process.Id != current.Id) //忽略當前進程 { //確認相同進程的程式運行位置是否一樣. if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @"\") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } 2,把Main ()函數改為如下代碼: static void Main() { if(RunningInstance()==null) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else { MessageBox.Show("已經運行了一個執行個體了。"); } }實現方式三:全域原子法,建立程式前,先檢查全域原子表中看是否存在特定原子A(建立時添加的),存在時停止建立,說明該程式已運行了一個執行個體;不存在則運行程式並想全域原子表中添加特定原子A;退出程式時要記得釋放特定的原子A哦,不然要到關機才會釋放。C#實現如下:1、申明WinAPI函數介面: [System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern UInt32 GlobalAddAtom(String lpString); //添加原子 [System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern UInt32 GlobalFindAtom(String lpString); //尋找原子 [System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); //刪除原子2、修改Main()函數如下: static void Main() { if (GlobalFindAtom("xinbiao_test") == 77856768) //沒找到原子"xinbiao_test" { GlobalAddAtom("xinbiao_test"); //添加原子"xinbiao_test" Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else { MessageBox.Show("已經運行了一個執行個體了。"); } }3、在FormClosed事件中添加如下代碼: GlobalDeleteAtom(GlobalFindAtom("xinbiao_test"));//刪除原子"xinbiao_test"以上為建立互斥程式(進程)的基本通用的思想,個人認為,第一種方法最好。以上所有代碼都在VS.NET2005 中測試通過。