使用Mutex運(yùn)行應(yīng)用程序的單個(gè)實(shí)例為了只允許運(yùn)行應(yīng)用程序的單個(gè)實(shí)例,我正在使用互斥鎖。代碼如下。這是正確的方法嗎?代碼中是否有任何缺陷?當(dāng)用戶第二次嘗試打開應(yīng)用程序時(shí),如何顯示已在運(yùn)行的應(yīng)用程序。目前(在下面的代碼中),我只是顯示另一個(gè)實(shí)例已在運(yùn)行的消息。 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");
}
}
- 3 回答
- 0 關(guān)注
- 435 瀏覽
添加回答
舉報(bào)
0/150
提交
取消