代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Terry{
class Program
{
private Mutex mutex = null;
public Program()
{
mutex = new Mutex(false, "TerrySoft_MUTEX");
if (!mutex.WaitOne(0, false))
{
mutex.Close();
mutex = null;
}
}
public void ReleaseMutex()
{
if (mutex != null)
{
mutex.ReleaseMutex();
mutex = null;
}
}
~Program()
{
ReleaseMutex();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Program cm = new Program();
if (cm.mutex == null)
{
MessageBox.Show("已经有一个程序在运行中。");
}
else
{
Application.Run(new frmStart());
}
cm.ReleaseMutex();//手工释放Mutex
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Terry{
class Program
{
private Mutex mutex = null;
public Program()
{
mutex = new Mutex(false, "TerrySoft_MUTEX");
if (!mutex.WaitOne(0, false))
{
mutex.Close();
mutex = null;
}
}
public void ReleaseMutex()
{
if (mutex != null)
{
mutex.ReleaseMutex();
mutex = null;
}
}
~Program()
{
ReleaseMutex();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Program cm = new Program();
if (cm.mutex == null)
{
MessageBox.Show("已经有一个程序在运行中。");
}
else
{
Application.Run(new frmStart());
}
cm.ReleaseMutex();//手工释放Mutex
}
}
}