方法一:使用 Microsoft 提供的互斥类Mutex
方法二:使用API函数实现
比较方法一与方法二,方法一在多个终端下仍会各自运行一个实例,但方法二可以这个问题。
/// <summary>
/// ApplicationStart 的摘要说明。
/// </summary>
static class ApplicationStart
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Mutex mutex = new Mutex(false, "MUTEX");
try
{
if (mutex.WaitOne(0, false))
{
Application.Run(new Form1());
}
//else
//{
// MessageBox.Show("已有进程在运行");
//}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
mutex.Close();
}
}
}
/// ApplicationStart 的摘要说明。
/// </summary>
static class ApplicationStart
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Mutex mutex = new Mutex(false, "MUTEX");
try
{
if (mutex.WaitOne(0, false))
{
Application.Run(new Form1());
}
//else
//{
// MessageBox.Show("已有进程在运行");
//}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
mutex.Close();
}
}
}
方法二:使用API函数实现
/// <summary>
/// ApplicationStart 的摘要说明。
/// </summary>
static class ApplicationStart
{
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = GetRunningInstance();
if (instance == null)
{
//初始化程序配置信息
//ApplicationSettings.Initialize();
Application.Run(new Form1());
}
else
{
HandleRunningInstance(instance);
}
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns>返回当前Process实例</returns>
public static Process GetRunningInstance()
{
//获取当前进程
Process currentProcess = Process.GetCurrentProcess();
string currentFileName = currentProcess.MainModule.FileName;
//创建新的 Process 组件的数组,并将它们与本地计算机上共享指定的进程名称的所有进程资源关联。
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
//遍历正在有相同名字运行的进程
foreach (Process process in processes)
{
if (process.MainModule.FileName == currentFileName)
{
if (process.Id != currentProcess.Id)//排除当前的进程
return process;//返回已启动的进程实例
}
}
return null;
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns>Process进程</returns>
//public static Process GetRunningInstance()
//{
//Process current = Process.GetCurrentProcess();
//Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
//foreach (Process process in processes)
//{
//忽略现有的例程
//if (process.Id != current.Id)
//确保例程从EXE文件运行
//if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
//返回另一个例程实例
//return process;
//}
//return null;
//}
/// <summary>
/// 获取窗口句柄
/// </summary>
/// <param name="instance">Process进程实例</param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
}
/// ApplicationStart 的摘要说明。
/// </summary>
static class ApplicationStart
{
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process instance = GetRunningInstance();
if (instance == null)
{
//初始化程序配置信息
//ApplicationSettings.Initialize();
Application.Run(new Form1());
}
else
{
HandleRunningInstance(instance);
}
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns>返回当前Process实例</returns>
public static Process GetRunningInstance()
{
//获取当前进程
Process currentProcess = Process.GetCurrentProcess();
string currentFileName = currentProcess.MainModule.FileName;
//创建新的 Process 组件的数组,并将它们与本地计算机上共享指定的进程名称的所有进程资源关联。
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
//遍历正在有相同名字运行的进程
foreach (Process process in processes)
{
if (process.MainModule.FileName == currentFileName)
{
if (process.Id != currentProcess.Id)//排除当前的进程
return process;//返回已启动的进程实例
}
}
return null;
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns>Process进程</returns>
//public static Process GetRunningInstance()
//{
//Process current = Process.GetCurrentProcess();
//Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
//foreach (Process process in processes)
//{
//忽略现有的例程
//if (process.Id != current.Id)
//确保例程从EXE文件运行
//if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
//返回另一个例程实例
//return process;
//}
//return null;
//}
/// <summary>
/// 获取窗口句柄
/// </summary>
/// <param name="instance">Process进程实例</param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
}
比较方法一与方法二,方法一在多个终端下仍会各自运行一个实例,但方法二可以这个问题。