要實現程序的互斥,通常有下面幾種方式,下面用 C# 語言來實現:
方法一:
使用線程互斥變量. 通過定義互斥變量來判斷是否已運行實例.
把program.cs文件里的Main()函數改為如下代碼:
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>
/// 應用程序的主入口點。
/// </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("已經運行了一個實例了。");
//IntPtr hdc = new IntPtr(1312810); // read from...
//bool flash = FlashWindow(hdc, true);
}
}
}
}
說明:程序中通過語句 System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);來創建一個互斥體變量run,其中"single_test"為互斥體名,在此方法返回時,如果創建了局部互斥體或指定的命名系統互斥體,則布爾值runone為true;如果指定的命名系統互斥體已存在,則為 false。已命名的互斥體是系統范圍的。
方法二:采用判斷進程的方式,我們在運行程序前,查找進程中是否有同名的進程,同時運行位置也相同程,如是沒有運行該程序,如果有就就不運行.在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("jiaao_test") == 77856768) //沒找到原子"jiaao_test"
{
GlobalAddAtom("jiaao_test"); //添加原子"jiaao_test"
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("已經運行了一個實例了。");
}
}
3.在FormClosed事件中添加如下代碼:
GlobalDeleteAtom(GlobalFindAtom("jiaao_test"));//刪除原子"jiaao_test"
方法四:通過進程判斷是否啟動:
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
//獲取當前進程的ID
int pId = Process.GetCurrentProcess().Id;
bool isRun = false;
foreach (Process p in Process.GetProcessesByName("CallMaster"))
{
//取得當前程序的進程,進行比較
if (Common.GetPath().ToLower() == p.MainModule.FileName.ToLower())
{
if (pId != p.Id)
{
isRun = true;
break;
}
}
}
if (isRun==true)
{
Application.Exit();
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
利用放射獲取當前應用程序的全路徑:
public static string GetPath()
{
return System.Reflection.Assembly.GetExecutingAssembly().Location;
}
方法五:通過線程互斥判斷是否啟動:
static class Program
{
private static System.Threading.Mutex mutex;
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mutex = new System.Threading.Mutex(true, "OnlyRun");
if (mutex.WaitOne(0, false))
{
Application.Run(new MainForm());
}
else
{
MessageBox.Show("程序已經在運行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
}
}
另附:c#中怎樣判斷一個程序是否正在運行?
if (System.Diagnostics.Process.GetProcessesByName("程序進程中的名稱").ToList().Count > 0)
{
//存在
}
else
{
//不存在
}