【发布时间】:2010-10-23 14:47:30
【问题描述】:
为了只允许应用程序的单个实例运行,我正在使用互斥锁。代码如下。这是正确的方法吗?代码有缺陷吗?
当用户第二次尝试打开应用程序时如何显示已经运行的应用程序。目前(在下面的代码中),我只是显示另一个实例已经在运行的消息。
static void Main(string[] args)
{
Mutex _mut = null;
try
{
_mut = Mutex.OpenExisting(AppDomain.CurrentDomain.FriendlyName);
}
catch
{
//handler to be written
}
if (_mut == null)
{
_mut = new Mutex(false, AppDomain.CurrentDomain.FriendlyName);
}
else
{
_mut.Close();
MessageBox.Show("Instance already running");
}
}
【问题讨论】: