为了只允许运行应用程序的单个实例,我正在使用互斥锁。代码如下。这是正确的方法吗?代码中是否有任何缺陷?
当用户第二次尝试打开应用程序时如何显示已经运行的应用程序。目前(在下面的代码中),我只是显示另一个实例已经在运行的消息。
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");
}
}