[DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int SW_SHOWMAXIMIZED = 3;
/// <summary>
/// 显示已运行的程序。
/// </summary>
public static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, SW_SHOWMAXIMIZED); //显示
SetForegroundWindow(instance.MainWindowHandle); //放到前端
}
定义一个HandleRunningInstance(Process instance)方法,负责显示已经运行的实例。
static void Main() { /*单例模式运行程序*/ Process instance = RunningInstance(); if (instance != null) { HandleRunningInstance(instance); return; } }
在Main方法中添加以上代码,负责检测是否已有实例在运行了。
以上代码来自互联网,我也忘记是看到谁的代码了。