int i = 0; 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) { i++; } } } if (i > 0) { MessageBox.Show("程序已经打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; }
System.Threading.Mutex mutex = new System.Threading.Mutex(false, "OnlyRunOnce");
//判断互斥体是否使用中。
bool Running = !mutex.WaitOne(0, false);
if (! Running)
{
Application.Run(new MainForm());
}
else
{
}
{
static Form form = null; private Form1()
{
} public static Form CreateInstance()
{
if(form == null)
{
form = new Form();
}
return form;
}
} private void button2_Click(object sender, System.EventArgs e)
{
Form form1 = Form1.CreateInstance();
form1.Show();
}
=========================================
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;using Zhzuo.WinGui;namespace Zhzuo
{
/// <summary>
/// AppStart 的摘要说明。
/// </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></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"></param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
}
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)
{
i++;
}
}
}
if (i > 0)
{
MessageBox.Show("程序已经打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}