Programe.cs
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[System.STAThread]
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
using (var mutex = new System.Threading.Mutex(false, "OneAtATimeDemo"))
{
// 可能其它程序实例正在关闭,所以可以等待几秒来让其它实例完成关闭
if (!mutex.WaitOne(System.TimeSpan.FromSeconds(3), false))
{
System.Windows.Forms.MessageBox.Show("Another app instance is running. Bye!");
return;
}
System.Windows.Forms.Application.Run(new Form1());
}
}
}