方法一:
static void Main()
{
string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
{
MessageBox.Show("系统已经在运行中...","系统警告",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Asterisk);
}
else
Application.Run(new Form1());
}
方法二:
//在Main()函数中
#region 仅允许运行一个实例
bool bOnlyOneInstance = false ;
Mutex mutex = new Mutex(true, Application.UserAppDataPath.Replace(@"/", "_"), out bOnlyOneInstance) ;
if ( !bOnlyOneInstance )
{
MessageBox.Show("系统已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) ;
return ;
}
//调用主窗体
//...
#endregion
方法三:
using “static”to have only one copy 这种方法没试过