解決C#程序只允許運(yùn)行一個(gè)實(shí)例的幾種方法詳解
更新時(shí)間:2013年05月20日 10:41:25 作者:
本篇文章是對(duì)C#中程序只允許運(yùn)行一個(gè)實(shí)例的幾種方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
本文和大家講一下如何使用C#來(lái)創(chuàng)建系統(tǒng)中只能有該程序的一個(gè)實(shí)例運(yùn)行。
要實(shí)現(xiàn)程序的互斥,通常有下面幾種方式,下面用 C# 語(yǔ)言來(lái)實(shí)現(xiàn):
方法一:
使用線程互斥變量. 通過(guò)定義互斥變量來(lái)判斷是否已運(yùn)行實(shí)例.
把program.cs文件里的Main()函數(shù)改為如下代碼:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace NetTools
{
static class Program
{
[DllImport("user32.dll")]
private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
[DllImport("user32.dll")]
private static extern bool FlashWindowEx(int pfwi);
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
bool runone;
System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);
if (runone)
{
run.ReleaseMutex();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FrmRemote frm = new FrmRemote();
int hdc = frm.Handle.ToInt32(); // write to ...
Application.Run(frm);
IntPtr a = new IntPtr(hdc);
}
else
{
MessageBox.Show("已經(jīng)運(yùn)行了一個(gè)實(shí)例了。");
//IntPtr hdc = new IntPtr(1312810); // read from...
//bool flash = FlashWindow(hdc, true);
}
}
}
}
說(shuō)明:程序中通過(guò)語(yǔ)句 System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);來(lái)創(chuàng)建一個(gè)互斥體變量run,其中"single_test"為互斥體名,在此方法返回時(shí),如果創(chuàng)建了局部互斥體或指定的命名系統(tǒng)互斥體,則布爾值runone為true;如果指定的命名系統(tǒng)互斥體已存在,則為 false。已命名的互斥體是系統(tǒng)范圍的。
方法二:采用判斷進(jìn)程的方式,我們?cè)谶\(yùn)行程序前,查找進(jìn)程中是否有同名的進(jìn)程,同時(shí)運(yùn)行位置也相同程,如是沒(méi)有運(yùn)行該程序,如果有就就不運(yùn)行.在C#中應(yīng)用System.Diagnostics名字空間中的Process類(lèi)來(lái)實(shí)現(xiàn),主要代碼如下:
1,在program.cs文件中添加函數(shù)如下:
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) //查找相同名稱(chēng)的進(jìn)程
{
if (process.Id != current.Id) //忽略當(dāng)前進(jìn)程
{ //確認(rèn)相同進(jìn)程的程序運(yùn)行位置是否一樣.
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 ()函數(shù)改為如下代碼:
static void Main()
{
if(RunningInstance()==null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("已經(jīng)運(yùn)行了一個(gè)實(shí)例了。");
}
}
方法三:全局原子法,創(chuàng)建程序前,先檢查全局原子表中看是否存在特定原子A(創(chuàng)建時(shí)添加的),存在時(shí)停止創(chuàng)建,說(shuō)明該程序已運(yùn)行了一個(gè)實(shí)例;不存在則運(yùn)行程序并想全局原子表中添加特定原子A;退出程序時(shí)要記得釋放特定的原子A哦,不然要到關(guān)機(jī)才會(huì)釋放。C#實(shí)現(xiàn)如下:
1.申明WinAPI函數(shù)接口
[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()函數(shù)如下:
static void Main()
{
if (GlobalFindAtom("jiaao_test") == 77856768) //沒(méi)找到原子"jiaao_test"
{
GlobalAddAtom("jiaao_test"); //添加原子"jiaao_test"
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("已經(jīng)運(yùn)行了一個(gè)實(shí)例了。");
}
}
3.在FormClosed事件中添加如下代碼:
GlobalDeleteAtom(GlobalFindAtom("jiaao_test"));//刪除原子"jiaao_test"
--------------------------------------*-------*--------*-----------------------------------------------
以上為創(chuàng)建互斥程序的基本通用的思想,個(gè)人認(rèn)為,第一種方法最簡(jiǎn)單。
要實(shí)現(xiàn)程序的互斥,通常有下面幾種方式,下面用 C# 語(yǔ)言來(lái)實(shí)現(xiàn):
方法一:
使用線程互斥變量. 通過(guò)定義互斥變量來(lái)判斷是否已運(yùn)行實(shí)例.
把program.cs文件里的Main()函數(shù)改為如下代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace NetTools
{
static class Program
{
[DllImport("user32.dll")]
private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
[DllImport("user32.dll")]
private static extern bool FlashWindowEx(int pfwi);
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
bool runone;
System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);
if (runone)
{
run.ReleaseMutex();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FrmRemote frm = new FrmRemote();
int hdc = frm.Handle.ToInt32(); // write to ...
Application.Run(frm);
IntPtr a = new IntPtr(hdc);
}
else
{
MessageBox.Show("已經(jīng)運(yùn)行了一個(gè)實(shí)例了。");
//IntPtr hdc = new IntPtr(1312810); // read from...
//bool flash = FlashWindow(hdc, true);
}
}
}
}
說(shuō)明:程序中通過(guò)語(yǔ)句 System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);來(lái)創(chuàng)建一個(gè)互斥體變量run,其中"single_test"為互斥體名,在此方法返回時(shí),如果創(chuàng)建了局部互斥體或指定的命名系統(tǒng)互斥體,則布爾值runone為true;如果指定的命名系統(tǒng)互斥體已存在,則為 false。已命名的互斥體是系統(tǒng)范圍的。
方法二:采用判斷進(jìn)程的方式,我們?cè)谶\(yùn)行程序前,查找進(jìn)程中是否有同名的進(jìn)程,同時(shí)運(yùn)行位置也相同程,如是沒(méi)有運(yùn)行該程序,如果有就就不運(yùn)行.在C#中應(yīng)用System.Diagnostics名字空間中的Process類(lèi)來(lái)實(shí)現(xiàn),主要代碼如下:
1,在program.cs文件中添加函數(shù)如下:
復(fù)制代碼 代碼如下:
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) //查找相同名稱(chēng)的進(jìn)程
{
if (process.Id != current.Id) //忽略當(dāng)前進(jìn)程
{ //確認(rèn)相同進(jìn)程的程序運(yùn)行位置是否一樣.
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 ()函數(shù)改為如下代碼:
復(fù)制代碼 代碼如下:
static void Main()
{
if(RunningInstance()==null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("已經(jīng)運(yùn)行了一個(gè)實(shí)例了。");
}
}
方法三:全局原子法,創(chuàng)建程序前,先檢查全局原子表中看是否存在特定原子A(創(chuàng)建時(shí)添加的),存在時(shí)停止創(chuàng)建,說(shuō)明該程序已運(yùn)行了一個(gè)實(shí)例;不存在則運(yùn)行程序并想全局原子表中添加特定原子A;退出程序時(shí)要記得釋放特定的原子A哦,不然要到關(guān)機(jī)才會(huì)釋放。C#實(shí)現(xiàn)如下:
1.申明WinAPI函數(shù)接口
復(fù)制代碼 代碼如下:
[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()函數(shù)如下:
復(fù)制代碼 代碼如下:
static void Main()
{
if (GlobalFindAtom("jiaao_test") == 77856768) //沒(méi)找到原子"jiaao_test"
{
GlobalAddAtom("jiaao_test"); //添加原子"jiaao_test"
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("已經(jīng)運(yùn)行了一個(gè)實(shí)例了。");
}
}
3.在FormClosed事件中添加如下代碼:
GlobalDeleteAtom(GlobalFindAtom("jiaao_test"));//刪除原子"jiaao_test"
--------------------------------------*-------*--------*-----------------------------------------------
以上為創(chuàng)建互斥程序的基本通用的思想,個(gè)人認(rèn)為,第一種方法最簡(jiǎn)單。
您可能感興趣的文章:
- 讓?xiě)?yīng)用程序只運(yùn)行一個(gè)實(shí)例的實(shí)現(xiàn)方法
- bat腳本實(shí)例實(shí)現(xiàn)只允許運(yùn)行一個(gè)實(shí)例(安裝程序、創(chuàng)建快捷方式腳本)
- C#判斷某程序是否運(yùn)行的方法
- C#確保只有一個(gè)實(shí)例在運(yùn)行的方法
- C#運(yùn)行時(shí)相互關(guān)系淺析
- C#編程中設(shè)置程序只可被運(yùn)行一次的方法
- C#簡(jiǎn)單實(shí)現(xiàn)防止多個(gè)程序運(yùn)行的方法
- C#如何防止程序多次運(yùn)行的技巧
- C# WinForm 判斷程序是否已經(jīng)在運(yùn)行,且只允許運(yùn)行一個(gè)實(shí)例,附源碼
相關(guān)文章
C#實(shí)現(xiàn)學(xué)員信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)學(xué)員信息管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
C#程序最小化到托盤(pán)圖標(biāo)操作步驟與實(shí)現(xiàn)代碼
設(shè)置窗體屬性showinTask=false;加notifyicon控件notifyIcon1,為控件notifyIcon1的屬性Icon添加一個(gè)icon圖標(biāo);添加窗體最小化事件(首先需要添加事件引用)接下來(lái)介紹實(shí)現(xiàn)代碼,感興趣的朋友可以研究下2012-12-12
c#訪問(wèn)this關(guān)鍵字和base關(guān)鍵字示例
this關(guān)鍵字引用類(lèi)的當(dāng)前實(shí)例。靜態(tài)成員方法中不能使用this關(guān)鍵字,this關(guān)鍵字只能在實(shí)例構(gòu)造函數(shù)、實(shí)例方法或?qū)嵗L問(wèn)器中使用。base關(guān)鍵字用于從派生類(lèi)中訪問(wèn)基類(lèi)的成員。下面學(xué)習(xí)一下這二個(gè)關(guān)鍵字的使用方法2014-01-01
Unity shader實(shí)現(xiàn)高斯模糊效果
這篇文章主要為大家詳細(xì)介紹了Unity shader實(shí)現(xiàn)高斯模糊效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
.net中前臺(tái)javascript與后臺(tái)c#函數(shù)相互調(diào)用問(wèn)題
.net中前臺(tái)javascript與后臺(tái)c#函數(shù)相互調(diào)用問(wèn)題...2007-12-12
C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解
這篇文章主要介紹了C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

