防止程序多开的两种方法
生活随笔
收集整理的這篇文章主要介紹了
防止程序多开的两种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【程序篇】防止程序多開的兩種方法
<a target=_blank href="http://bbs.cskin.net/forum.php?mod=viewthread&tid=105&fromuid=2446">http://bbs.cskin.net/forum.php?mod=viewthread&tid=105&fromuid=2446</a>
(出處: CSkin論壇)
互斥對象防止程序多開
private void Form1_Load(object sender, EventArgs e)
{bool Exist;//定義一個bool變量,用來表示是否已經運行//創建Mutex互斥對象System.Threading.Mutex newMutex = new System.Threading.Mutex(true, "僅一次", out Exist);if (Exist)//如果沒有運行{newMutex.ReleaseMutex();//運行新窗體}else{MessageBox.Show("本程序一次只能運行一個實例!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//彈出提示信息this.Close();//關閉當前窗體}
}
進程檢查 private void Form1_Load(object sender, EventArgs e) {//獲取當前活動進程的模塊名稱string moduleName = Process.GetCurrentProcess().MainModule.ModuleName;//返回指定路徑字符串的文件名string processName = System.IO.Path.GetFileNameWithoutExtension(moduleName);//根據文件名創建進程資源數組Process[] processes = Process.GetProcessesByName(processName);//如果該數組長度大于1,說明多次運行if (processes.Length > 1){MessageBox.Show("本程序一次只能運行一個實例!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//彈出提示信息this.Close();//關閉當前窗體}
進程檢查 private void Form1_Load(object sender, EventArgs e) {//獲取當前活動進程的模塊名稱string moduleName = Process.GetCurrentProcess().MainModule.ModuleName;//返回指定路徑字符串的文件名string processName = System.IO.Path.GetFileNameWithoutExtension(moduleName);//根據文件名創建進程資源數組Process[] processes = Process.GetProcessesByName(processName);//如果該數組長度大于1,說明多次運行if (processes.Length > 1){MessageBox.Show("本程序一次只能運行一個實例!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//彈出提示信息this.Close();//關閉當前窗體}
總結
以上是生活随笔為你收集整理的防止程序多开的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最全的C#图片处理类ImageHelpe
- 下一篇: Apache shutdown unex