static class Program { static bool createdNew = false; static Mutex me = new Mutex(true, "MyAppMutex",out createdNew); /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (!createdNew) MessageBox.Show("已经运行!"); else Application.Run(new Form1()); } }[align=center]***************************************** 本内容使用CSDN小秘书回复 每天回帖即可得10分可用分! *****************************************[/align]
这一段代码写在哪?是在form_load内么
//你需要的是exe程序运行一个实例Process p =Process.GetProcessByName("程序名"); if(p!=null) { //存在实例,退出 } else {}
/// <summary>
/// 互斥实例(定义一个变量)
/// </summary>
private Mutex m_mt = null;/// <summary>
/// 单实例设置(构造函数调用他)
/// </summary>
private void MutexRun()
{
bool bRun;
m_mt = new Mutex(true, "TestRun", out bRun);
if (!bRun)
{
// 如果已经有一个实例在运行,则将该实例窗口置前
IntPtr Hander = CommonGenerator.FindWindow(null, "窗口标题");
if (Hander != IntPtr.Zero)
{
CommonGenerator.SetForegroundWindow(Hander);
}
m_mt.Close();
Environment.Exit(1);
return;
}
}
{
static bool createdNew = false;
static Mutex me = new Mutex(true, "MyAppMutex",out createdNew);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (!createdNew)
MessageBox.Show("已经运行!");
else
Application.Run(new Form1());
}
}[align=center]*****************************************
本内容使用CSDN小秘书回复
每天回帖即可得10分可用分!
*****************************************[/align]
这一段代码写在哪?是在form_load内么
//你需要的是exe程序运行一个实例Process p =Process.GetProcessByName("程序名");
if(p!=null)
{
//存在实例,退出
}
else
{}
static void Main(string[] args)
{
bool flag=false;
System.Threading.Mutex mutex=new System.Threading.Mutex(true,"MutexExample",out flag);
if(flag) {
Console.Write("Running");
}
else {
Console.Write("Another is Running");
}
Console.ReadLine();
}终于找到正解了,谢谢大家的帮助!