让程序以单一实例运行

前端之家收集整理的这篇文章主要介绍了让程序以单一实例运行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#region 让程序以单一实例运行
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

private const int SW_RESTORE = 9;

/// <summary> /// 在 main() 中调用:if (!Common.RunSingleInstance()) return; /// </summary> /// <returns></returns> public static bool RunSingleInstance() { string proc = Process.GetCurrentProcess().ProcessName; Process[] processes = Process.GetProcessesByName(proc); if (processes.Length > 1) { Process p = Process.GetCurrentProcess(); int n = 0; if (processes[0].Id == p.Id) { n = 1; } IntPtr hWnd = processes[n].MainWindowHandle; if (IsIconic(hWnd)) { ShowWindowAsync(hWnd,SW_RESTORE); } SetForegroundWindow(hWnd); return false; } return true; } #endregion

原文链接:https://www.f2er.com/javaschema/287072.html

猜你在找的设计模式相关文章