摘要

接着介绍项目中用到的一些方法,在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. 使用InstallShield打包windriver驱动-转

    转自:http://blog.csdn.net/weixin_29796711/article/details/72822052 用户在使用我们用windriver开发的硬件驱动时,需要先安装wind ...

  2. python运行execjs解密js

    [转]http://www.knowsky.com/1041161.html python 记一次计算qzonetoken经历 之前用python写了个发表说说的爬虫,但最近发现在post数据时返回不 ...

  3. bulk_write&Replace_one

    ns=[]ns.append(ReplaceOne({'ip': ok['ip']}, ok, upsert=True))#更新插入 if len(ns) > 0: res = coll.bul ...

  4. C# 压缩文件 的创建

    using System;using System.IO.Compression; using System.Collections.Generic;using System.Linq;using S ...

  5. Android Studio之代码提示快捷键冲突设置

    1.原代码提示快捷键为:Ctrl+空格,与Windows输入法冲突,所以将代码提示快捷键设置为:Ctrl+反斜杠.

  6. 公司软raid问题

    RAID的技术介绍: stripe width(条带宽度):RAID中的磁盘数,就是组成这个stripe的磁盘数.如,4个磁盘组成的RAID 0,条带宽度就是4. stripe depth(条带深度) ...

  7. JVM(3)对象A和B循环引用,最后会不会不被GC回收?-------关于Java的GC机制

    ①首先说一下,GC里边在JVM其中是使用的ROOT算法,ROOT算法,什么称作为ROOT呢,就是说类的静态成员,静态成员就是static修饰的那种,是"根"的一个,根还包含方法中的 ...

  8. C/C++ ASCII码表

    C: dec oct hex ch   dec oct hex ch   dec oct hex ch   dec oct hex ch 0 0 00 NUL (null) 32 40 20 (spa ...

  9. android:怎么实现一个控件与另一个指定控件左对齐

    https://segmentfault.com/q/1010000003905460?_ea=425861 针对你这种情况,最简单的一种办法是,设置两个TextView的宽度为固定值,且相等. Li ...

  10. shared_ptr(作为局部变量返回)

    智能指针:shared_ptr 1.一个局部的shared_ptr 作为返回值过程:当shared_ptr 被创建的时候,自身的引用计数 +1,当前引用计数为 1 , 按值返回以后 引用计数 + 1 ...