为了只允许运行一个应用程序的单个实例,我使用了互斥。代码如下所示。这是正确的做法吗?代码中有什么缺陷吗?
当用户再次尝试打开应用程序时,如何显示已经运行的应用程序。目前(在下面的代码中),我只是显示一条消息,表明另一个实例已经在运行。
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");
}
}
https://stackoverflow.com/questions/819773
复制相似问题