[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# 控制职能运行单一实例,再次运行显示已经运行的实例
有这么个需求,软件只能运行一个实例,软件运行后可以让其隐藏运行 再次运行这个软件的时候就让正在运行的实例显示出来 ================================= 当软件隐藏后没办法 ...
随机推荐
- 使用 scm-manager 搭建 git/svn 代码管理仓库(二)
主要介绍scm的配置. 1.配置为在Windows服务中启动scm-manager的启动方式有多种,可以在DOS(即命令行CMD模式)中启动,也可以在Windows服务中启动. 下面我们采用Windo ...
- aliyun EC2配置利用filezilla配置ftp服务
项目需要在阿里云EC2虚拟主机上配置ftp服务器,看了阿里云的教程可以使用filezilla配置,但一直遇到了一些问题.现记录一些步骤,避免以后出现类似问题. 1安装filezilla server ...
- [转]使用 mitmproxy + python 做拦截代理
使用 mitmproxy + python 做拦截代理 本文是一个较为完整的 mitmproxy 教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 ...
- 查找网内活跃IP和自动传输文本
ifconfig p32p1|egrep -o "broadcast [^ ]*" |grep -o "[0-9.]*"grep -o "broadc ...
- Windows下RabbitMQ安装及配置
下载rabbitmq_server以及Erlang OTP平台 安装好了启动服务就行了 也可用命令 net start RabbitMQ 或 net stop RabbitMQ 配置用户添加环境变 ...
- 【算法】后缀自动机(SAM) 初探
[自动机] 有限状态自动机的功能是识别字符串,自动机A能识别字符串S,就记为$A(S)$=true,否则$A(S)$=false. 自动机由$alpha$(字符集),$state$(状态集合),$in ...
- rhev 虚拟化
引用自:https://blog.csdn.net/Jmilk/article/details/50964121#rhev-hhypervisor-%E8%99%9A%E6%8B%9F%E6%9C%B ...
- Java基础常见英语词汇(共70个)
——————————ASP.Net+Android+IOS开发..Net培训.期待与您交流!—————————— Java英文单词 OO: object—oriented ,面向对象 OOP:obje ...
- Redis学习笔记10--Redis主从复制
redis主从复制配置和使用都非常简单.通过主从复制可以允许多个slave server拥有和master server相同的数据库副本.下面是关于redis主从复制的一些特点: 1.ma ...
- node.js之nodemon 代码热更新 修改代码后服务器自动重启
1.安装nodemon: npm install -g nodemon //全局安装 npm install nodemon --save //局部安装 2.在项目根目录下创建 nodemon.jso ...