摘要

接着介绍项目中用到的一些方法,在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,如果已运行则将窗口置前的更多相关文章

  1. C#像运行一个exe 程序一样运行一个dll文件

    [DllImport("kernel32.dll")] public static extern int WinExec(string exeName, int operType) ...

  2. ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前

    原文:ASP.NET查询页面设置form的action属性只弹出一个页面,并且每次将页面设置到最前 背景 当数据量大.查询条件复杂,多样多的时候,我们可能需要单独做一个查询界面,当用户选择设置了相关的 ...

  3. win应用只允许单个实例运行,并将已运行实例窗口置顶

    关键词:windows,c++,桌面应用,单个实例,窗口置顶 目标:1.判断本程序是否已有一个实例在运行.2.若有,则激活已在运行的实例(将其窗口置顶),并退出当前运行. 1.使用semaphore来 ...

  4. 只运行一个exe应用程序的使用案例

    应用程序的exe启动设置 using System;using System.Diagnostics;using System.Reflection;using System.Runtime.Inte ...

  5. 只能运行一个程序,禁止运行多个相同的程序 C#

    原文发布时间为:2009-04-06 -- 来源于本人的百度文章 [由搬家工具导入] Program.cs 里面改成如下: static void Main()        {            ...

  6. C#让应用程序只运行一个实例的几种方法

    一 判断是否有相同的实例已经运行 1 根据“Mutex”判断是否有相同的实例在运行 /// <returns>已有实例运行返回true,否则为false</returns>pu ...

  7. C++ 多用户模式下同一个exe只能运行一次

    1.有时候会遇到多用户模式下一不小心会运行多个exe的问题,所以程序中添加一下代码解决这个问题: BOOL CtestDialogApp::InitInstance() { char exeFullP ...

  8. HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo

    26 集群使用初步 HDFS的设计思路 l 设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: l 在大数据系统中作用: 为各类分布式 ...

  9. c# 控制职能运行单一实例,再次运行显示已经运行的实例

    有这么个需求,软件只能运行一个实例,软件运行后可以让其隐藏运行 再次运行这个软件的时候就让正在运行的实例显示出来 ================================= 当软件隐藏后没办法 ...

随机推荐

  1. 如何生成能在没有安装opencv库及vs2010环境的电脑上运行的exe文件

    项目基本算法已经完成,甲方需要一个可以运行的demo.目前,程序能在自己的电脑上正常运行.移植到其他win7系统上,运行失败. 寻找各种解决办法,baidu找到两个办法: 1.使用静态链接的方法,这种 ...

  2. BAT脚本加防火墙455端口

    @echo off mode con: cols=85 lines=30 :NSFOCUSXA title WannaCry勒索病毒安全加固工具 color 0A cls echo. echo. ec ...

  3. IIS部署常见问题总结

    1.通过网站子目录访问后台管理 场景:前台和后台分为两个项目开发,域名只有一个(t.cn),作为前台的地址,后台的地址希望指定为 t.cn/admin 项目部署的时候,需要把后台项目转化为/admin ...

  4. 10 Go 1.10 Release Notes

    Go 1.10 Release Notes Introduction to Go 1.10 Changes to the language Ports Tools Default GOROOT &am ...

  5. UFLDL 教程学习笔记(一)

    ufdl的新教程,从基础学起.第一节讲的是线性回归.主要目的是熟悉目标函数,计算梯度和优化. 按着教程写完代码后,总是编译出错,一查是mex的原因,实在不想整了. 这位博主用的是向量,比较简洁:htt ...

  6. java 异常匹配

    抛出异常的时候,异常处理系统会安装代码书写顺序找出"最近"的处理程序. 找到匹配的程序后,它就认为异常将得到清理,然后就不再继续查找. 查找的时候并不要求抛出的异常同处理程序的异常 ...

  7. .NET Core 项目经验总结:项目结构介绍 (一)

    原文地址(个人博客):http://www.gitblogs.com/Blogs/Details?id=384b4249-15e4-41bf-9cf7-44a3e1e51885 作为一个.NET We ...

  8. 数学之美——HMM模型(一)介绍

    一直想写点关于数学方面的blog,这对于数据挖掘分析,NLP处理等都有着比较重要的作用,之前在CSDN上想写点HMM方面的文章,一直没写成,最近几天终于抽点时间完成了HMM的文章,加以整理,遂有这个系 ...

  9. PHP字符串指定位置插入字符串

    1.substr_replace(string,replacement,start,length);需插入时设置length为0即可 string 必需.规定要检查的字符串. replacement ...

  10. jbox小型交互表单(ajax)

    function updateval(id){ var url = '${ctx}/zbapp/ranking/zbappRankingList/form?id=${zbappRankingList. ...