在主程序的 Program.cs 中, 按照下面代码进行修改即可
static class Program
{
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process[] processes = Process.GetProcesses();
Process currentProcess = Process.GetCurrentProcess();
foreach (Process p in processes)
{
if (p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id)
{
System.IntPtr handler = p.MainWindowHandle;
ShowWindowAsync(handler, 4);
Application.Exit();
Environment.Exit(1);
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}