这里列举了比较常见的方法:
1、
static void Main()
{
bool initiallyOwned = true;
bool isCreated;
Mutex m = new Mutex(initiallyOwned,"MyTest",out isCreated);
if (!(initiallyOwned && isCreated))
{
MessageBox.Show("已经有相同的实例在运行。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Application.Exit();
}
else
{
Application.Run(new MainForm());
}
}
2、
static void Main()
{
int ProceedingCount = 0;
Process[] ProceddingCon = Process.GetProcesses();
foreach(Process IsProcedding in ProceddingCon)
{
if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName)
{
ProceedingCount += 1;
}
}
if(ProceedingCount > 1)
{
MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new MainForm());
}
}
3、
private static bool CheckInstance()
{
string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
{
return true;
}
else
{
return false;
}
}
1、
static void Main()
{
bool initiallyOwned = true;
bool isCreated;
Mutex m = new Mutex(initiallyOwned,"MyTest",out isCreated);
if (!(initiallyOwned && isCreated))
{
MessageBox.Show("已经有相同的实例在运行。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Application.Exit();
}
else
{
Application.Run(new MainForm());
}
}
2、
static void Main()
{
int ProceedingCount = 0;
Process[] ProceddingCon = Process.GetProcesses();
foreach(Process IsProcedding in ProceddingCon)
{
if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName)
{
ProceedingCount += 1;
}
}
if(ProceedingCount > 1)
{
MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new MainForm());
}
}
3、
private static bool CheckInstance()
{
string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
{
return true;
}
else
{
return false;
}
}