用互斥对象来处理,参考如下代码: using System; using System.Collections.Generic; using System.Windows.Forms; using System.Threading; namespace MyProgram { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { bool vCreateNew; Mutex vMutex = new Mutex(false, "MyMutex", out vCreateNew); if (vCreateNew) { vMutex.WaitOne(); } else { MessageBox.Show("Zswang 已经路过", "提示信息:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
public const int WS_SHOWNORMAL = 1; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Process test = RunningInstance(); if (test == null) { //Go on your code } else { MessageBox.Show("Application already running!"); } } public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { if (Assembly.GetExecutingAssembly().Location.Replace("/ ", "\\ ") == current.MainModule.FileName) { return process; } } } return null; }
最简单的限制,窗口查找,如果找到自己窗口一致的(通过类名,或者窗口标题),就提示重复打开。
解决方法:
通过窗口标题的,可以用SetWindowText修改掉窗口标题。听过类名的,简单的把FindWindow(Ex)后面的判断跳转修改下就ok了。
限制2:
通过命名的内核对象来防止重复打开。
解决方法:
把查找内核对象后面的非零判断跳转修改下就ok了。
限制3:
通过独占文件访问来防止重复打开。
解决方法:
1-修改程序,阻止独占访问
2-复制客户端到另外的文件夹,在另外的文件夹打开~
限制4:
通过绑定特定的通讯端口,阻止重复启动客户端
解决方法:
把绑定端口的那部分去掉。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading; namespace MyProgram
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool vCreateNew;
Mutex vMutex = new Mutex(false, "MyMutex", out vCreateNew);
if (vCreateNew)
{
vMutex.WaitOne();
}
else
{
MessageBox.Show("Zswang 已经路过",
"提示信息:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
public const int WS_SHOWNORMAL = 1; /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Process test = RunningInstance();
if (test == null)
{
//Go on your code
}
else
{
MessageBox.Show("Application already running!");
}
} public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/ ", "\\ ") ==
current.MainModule.FileName)
{
return process;
}
}
}
return null;
}