加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

让程序以单一实例运行

发布时间:2020-12-13 20:15:57 所属栏目:百科 来源:网络整理
导读:#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")] p

#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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读