有时我们需要只允许运行应用程序的一个实例,当进程启动时,如果发现应用程序的一个实例在运行,就自动停止运行。我们通常通过Mutex互斥体在Main函数中实现,通常的写法是:
[STAThread]
static void Main()
{
bool createNew;
using (System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out createNew))
{
if (createNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("Only one instance of this application is allowed!");
}
}
}
static void Main()
{
bool createNew;
using (System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out createNew))
{
if (createNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("Only one instance of this application is allowed!");
}
}
}
我们在寻找答案时,往往来去匆匆,根本不去理会Mutex的特性和注意事项。经过简单的测试,OK,拿来就用。此时我们忽略了一个重要的前提条件:Mutex的命名规则。以上的写法在单用户下运行没有问题;在多用户下,每个用户都能启动一个实例,也就不能保证单实例运行了。
如果需要在终端机服务器上使用,并且只允许一个实例的话,请使用下面的写法:
[STAThread]
static void Main()
{
bool createNew;
try
{
using (System.Threading.Mutex m = new System.Threading.Mutex(true, "Global\\" + Application.ProductName, out createNew))
{
if (createNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("Only one instance of this application is allowed!");
}
}
}
catch
{
MessageBox.Show("Only one instance of this application is allowed!");
}
}
static void Main()
{
bool createNew;
try
{
using (System.Threading.Mutex m = new System.Threading.Mutex(true, "Global\\" + Application.ProductName, out createNew))
{
if (createNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
MessageBox.Show("Only one instance of this application is allowed!");
}
}
}
catch
{
MessageBox.Show("Only one instance of this application is allowed!");
}
}
以下是MSDN的说明: