ApplicationStart.cs 文件
=========================================
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using Zhengzuo.WinGui;
namespace Zhengzuo
{
/// <summary>
/// ApplicationStart 的摘要说明。
/// </summary>
public 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()
{
Process instance = GetRunningInstance();
if(instance == null)
{
//初始化程序配置信息
//ApplicationSettings.Initialize();
Application.Run(new MainForm());
}
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);
}
}
}
我介绍两个主流的方法。
方法一:使用Mutex来进行
1. 首先要添加如下的namespace:
using System.Threading;
2. 修改系统Main函数,大致如下:
bool bCreatedNew;
//Create a new mutex using specific mutex name
Mutex m =new Mutex( false, "myUniqueName", out bCreatedNew );
if( bCreatedNew )
Application.Run(new yourFormName());
如上面编码就可以了,要注意的一点是,在给Mutex起名字的时候,不要太简单,以防止和其他程序的Mutex重复,从而达不到所预想的效果。
方法二:使用Process来进行
1. 首先要添加如下的namespace:
using System.Diagnostics;
using System.Reflection;
2. 添加如下函数:
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}