在程序的main函數中加入以下代碼
bool createdNew;
System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName", out createdNew);
if (createdNew)
{
Application.Run(new LoginForm());
instance.ReleaseMutex();
}
else
{
Application.Exit();
}
還可以寫成以下形式,一個窗體只能啟動一次
Form1 a = new Form1();
bool createdNew;
System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName", out createdNew);
if (createdNew)
{
a.ShowDialog();
instance.ReleaseMutex();
}
else
{
a.Close();
}