Process[] allProcess = Process.GetProcesses();//所有进程 foreach (Process p in allProcess) { if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())//mainAppExe你的进程名 { ... } }
static void Main() { // get the name of our process string proc = Process.GetCurrentProcess().ProcessName; // get the list of all processes by that name Process[] processes = Process.GetProcessesByName(proc); // if there is more than one process if (processes.Length > 1) { MessageBox.Show("程序已经在运行中","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } else { Application.Run(new Form1()); } }
foreach (Process p in allProcess)
{ if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())//mainAppExe你的进程名
{
...
}
}
{
// get the name of our process
string proc = Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes = Process.GetProcessesByName(proc);
// if there is more than one process
if (processes.Length > 1)
{
MessageBox.Show("程序已经在运行中","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else
{
Application.Run(new Form1());
}
}
导入命名空间:
(再Program类中)
using System.Windows.Forms;
using System.Threading; static class Program
{
private static Mutex mutex;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mutex = new Mutex(true, "OnlyRun");
if (mutex.WaitOne(0, false))
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("程序已经运行!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Application.Exit();
}
}
}
我以前用的是一个全局变量,如果程序运行,这个变量为NULL,
只有在这个变量为NULL时,才实例化。
if (BI.KillMore())
{
MessageBox.Show(BI.strError);
}
else
{
Application.Run(new Login());
} public static bool KillMore()
{
int length = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
return length > 1;
}//没想到刚开始学的一点小东西还有用途
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
{
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;namespace FallingGold
{
static class Program
{
private static Mutex mutex;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mutex = new Mutex(true, "一个");
//保证只有一个实例运行
if (mutex.WaitOne(0, false))
{
Application.Run(new MaimForm());
}
}
}
}
{
bool createNew;
string name = Process.GetCurrentProcess().ProcessName + "Mutex";
using (Mutex mtx = new Mutex(true, name, out createNew))
{
if (!createNew) return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}