C#只能運行一個執行個體程式的方法

來源:互聯網
上載者:User
互斥進程(程式), 簡單點說,就是在系統中只能有該程式的一個執行個體運行. 現在很多軟體都有這功能,如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 中測試通過。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.