[Winform]只允许运行一个exe,如果已运行则将窗口置前
摘要
接着介绍项目中用到的一些方法,在winform中,打好包,有时并不允许运行多个客户端,要保证只有一个客户端运行。如果已经运行了,则弹出已运行的窗口,使其展示。
方法
判断是否有相同的进程
/// <summary>
/// 获取当前是否具有相同进程。
/// </summary>
/// <returns></returns>
public static Process GetRunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
//确保例程从EXE文件运行
if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
return process;
}
return null;
}
在Main函数中,进行判断
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
try
{ Process instance = GetRunningInstance();
Application.SetCompatibleTextRenderingDefault(false);
if (instance == null)
{
Run();
}
else
{
ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOACTIVATE); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端
}
}
catch (Exception ex)
{
LogInfoData.WriteLog(new LogInfo { Message = ex.Message, Op = "start_app_err" });
MessageBox.Show("系统启动出现异常,请重新启动");
}
}
windows API
private const int SW_HIDE = ;
private const int SW_NORMAL = ;
private const int SW_MAXIMIZE = ;
private const int SW_SHOWNOACTIVATE = ;
private const int SW_SHOW = ;
private const int SW_MINIMIZE = ;
private const int SW_RESTORE = ;
private const int SW_SHOWDEFAULT = ;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
在使用SetForegroundWindow需要注意以下几点:
The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
- The process is the foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The process is being debugged.
- The foreground process is not a Modern Application or the Start Screen.
- The foreground is not locked (see LockSetForegroundWindow).
- The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
- No menus are active.
An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.
A process that can set the foreground window can enable another process to set the foreground window by calling the AllowSetForegroundWindowfunction. The process specified by dwProcessId loses the ability to set the foreground window the next time the user generates input, unless the input is directed at that process, or the next time a process calls AllowSetForegroundWindow, unless that process is specified.
The foreground process can disable calls to SetForegroundWindow by calling the LockSetForegroundWindow function.
地址:https://msdn.microsoft.com/en-us/library/ms633539(v=VS.85).aspx
这个进程是前台进程。
这个进程是由前台进程的开始。
进程中收到的最后一个输入事件。
没有前台进程。
前台进程正在被调试。
前置不被锁定(见LockSetForegroundWindow)。
前置锁定超时已过期(看到SPI_GETFOREGROUNDLOCKTIMEOUTSystemParametersInfo)
需要满足上面一条即可,比如如果这个进程是后台进程,则不会起作用。可以在windows任务管理器中,查看是否在窗口最小化的时候,变成了后台进程。比如,常用的Hide方法,会是窗口变为后台进程,而窗口最下话操作,则不会改变进程状态,扔是前台进程。
this.WindowState = FormWindowState.Minimized;
[Winform]只允许运行一个exe,如果已运行则将窗口置前的更多相关文章
- C#像运行一个exe 程序一样运行一个dll文件
[DllImport("kernel32.dll")] public static extern int WinExec(string exeName, int operType) ...
- ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前
原文:ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前 背景 当数据量大.查询条件复杂,多样多的时候,我们可能需要单独做一个查询界面,当用户选择设置了相关的 ...
- win应用只允许单个实例运行,并将已运行实例窗口置顶
关键词:windows,c++,桌面应用,单个实例,窗口置顶 目标:1.判断本程序是否已有一个实例在运行.2.若有,则激活已在运行的实例(将其窗口置顶),并退出当前运行. 1.使用semaphore来 ...
- 只运行一个exe应用程序的使用案例
应用程序的exe启动设置 using System;using System.Diagnostics;using System.Reflection;using System.Runtime.Inte ...
- 只能运行一个程序,禁止运行多个相同的程序 C#
原文发布时间为:2009-04-06 -- 来源于本人的百度文章 [由搬家工具导入] Program.cs 里面改成如下: static void Main() { ...
- C#让应用程序只运行一个实例的几种方法
一 判断是否有相同的实例已经运行 1 根据“Mutex”判断是否有相同的实例在运行 /// <returns>已有实例运行返回true,否则为false</returns>pu ...
- C++ 多用户模式下同一个exe只能运行一次
1.有时候会遇到多用户模式下一不小心会运行多个exe的问题,所以程序中添加一下代码解决这个问题: BOOL CtestDialogApp::InitInstance() { char exeFullP ...
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...
- c# 控制职能运行单一实例,再次运行显示已经运行的实例
有这么个需求,软件只能运行一个实例,软件运行后可以让其隐藏运行 再次运行这个软件的时候就让正在运行的实例显示出来 ================================= 当软件隐藏后没办法 ...
随机推荐
- Javascript - Vue - 指令
指令 v-cloak 解决闪烁,闪烁是指在网速较慢的情况下可能会出现插值表达式{{}}还没有填充数据时会把该表达式直接显示在页面上,如果不希望看到插值表达式则可以使用v-cloak指令,具体做法如下 ...
- Ettercap之ARP+DNS欺骗
1.网络攻击拓扑环境 网关:192.168.133.2 攻击者:192.168.133.128 受害者:192.168.133.137 2.原理讲解 ARP欺骗 简介:ARP(Address Reso ...
- PyTorch学习系列(九)——参数_初始化
from:http://blog.csdn.net/VictoriaW/article/details/72872036 之前我学习了神经网络中权值初始化的方法 那么如何在pytorch里实现呢. P ...
- RabbitMQ Headers Exchange示例
(1).发布者 var connectionFactory = new ConnectionFactory() { HostName="192.168.205.128",UserN ...
- Redis常见业务场景应用
一定时间范围内不可重复发短信问题 Redis实现消息队列 Redis实现Session共享 ...
- springcloud中Feign配置详解
Spring Cloud中Feign配置详解 到目前为止,小伙伴们对Feign的使用已经掌握的差不多了,我们在前文也提到Feign是对Ribbon和Hystrix的整合,那么在Feign中,我们要如何 ...
- 002_docker构建zookeeper环境
最近因为要维护公司zk环境,所以自己先得搞一套先玩玩 git地址=>https://github.com/jplock/docker-zookeeper/tree/v3.4.9 一.build ...
- TinyHttpd代码解析
十一假期,闲来无事.看了几个C语言开源代码.http://www.cnblogs.com/TinyHttpd 这里本来想解析一下TinyHttpd的代码,但是在网上一搜,发现前辈们已经做的很好了.这里 ...
- 利用MYSQL的加密解密办法应对三级安全等级保护
-- 创建测试表 drop table if EXISTS t_passwd_2; create table t_passwd_2(pass1 varchar(64)); -- 对身份证号加密inse ...
- Yeoman generator
使用Yeoman generator来规范工程的初始化 前言 随着开发团队不断发展壮大,在人员增加的同时也带来了协作成本的增加:业务项目越来越多,类型也各不相同.常见的类型有基础组件.业务组件.基于R ...