应用程序的exe启动设置

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace OnlyRunOneEXE
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#region 第一种方式
//if (IsExistRunApplicationOrEXE())
//{
// MessageBox.Show("软件已经在运行!!!", "信息提示:");
// return;
//}
//else
//{
// Application.Run(new Form1());
//}
#endregion

#region MyRegion

Process process = RunningInstance();
if (process != null)
{
//MessageBox.Show("软件已经在运行!!!", "信息提示:");
HandleRunningInstance(process);
}
else
{
Application.Run(new Form1());
}
//Application.Run(new Form1());
#endregion
}

#region 第一种方式
public static bool IsExistRunApplicationOrEXE()
{
Mutex mutex = new Mutex(true, "OnlyRunOneEXE", out bool isCreatedNew);
Process[] processArrs = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (processArrs.Length > 2 || !isCreatedNew)
{
return true;
}
return false;
}

#endregion

#region 第二种方式
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

private static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//如果实例已经存在则忽略当前进程
if (process.Id != current.Id)
{
//保证要打开的进程同已经存在的进程来自同一文件路径
if (Assembly.GetExecutingAssembly().Location.Replace("/", @"\") == current.MainModule.FileName)
{
//返回已经存在的进程
return process;

}
}
}
return null;
}

private static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端
}

//[DllImport("User32.dll")]
////返回值:如果窗口原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。
//private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
////API cmdShow 常数定义
//private const int SW_HIDE = 0;
//private const int SW_NORMAL = 1; //正常弹出窗体
//private const int SW_MAXIMIZE = 3; //最大化弹出窗体
//private const int SW_SHOWNOACTIVATE = 4;
//private const int SW_SHOW = 5;
//private const int SW_MINIMIZE = 6;
//private const int SW_RESTORE = 9;
//private const int SW_SHOWDEFAULT = 10;
#endregion
}
}

只运行一个exe应用程序的使用案例的更多相关文章

  1. vc++高级班之窗口篇[4]---让程序只运行一个实例

      大家都看过或者使用过类似只运行一个实例的程序,比如:QQ游戏.部分浏览器 等等! 让一个程序只运行一个实例的方法有多种,但是原理都类似,也就是在程序创建后,有窗口的程序在窗口创建前, 检查系统中是 ...

  2. Linux编程之《只运行一个实例》

    概述 有些时候,我们要求一个程序在系统中只能启动一个实例.比如,Windows自带的播放软件Windows Medea Player在Windows里就只能启动一个实例.原因很简单,如果同时启动几个实 ...

  3. VC只运行一个程序实例

    方法有很多,以下只是提供一种用的多的 一. 单文档程序 在程序App类的InitInstance中添加如下代码 BOOL CDDZApp::InitInstance() { /*只运行一个实例*/ / ...

  4. C# JabLib系列之如何保证只运行一个应用程序的实现

    保证只运行一个应用程序的C#实现: using System;using System.Collections.Generic;using System.Linq;using System.Windo ...

  5. 如何让Windows程序只运行一个程序实例?

    要实现VC++或者MFC只运行一个程序实例,一般采用互斥量来实现,即首先用互斥量封装一个只运行一个程序实例的函数接口: HANDLE hMutex = NULL; void MainDlg::RunS ...

  6. VC 实现程序只运行一个实例,并激活已运行的程序

    转载:http://blog.sina.com.cn/s/blog_4b44e1c00100bh69.html 进程的互斥运行:CreateMutex函数实现只运行一个程序实例 正常情况下,一个进程的 ...

  7. WinFrom 只启动一个exe,并且获得焦点

    只启动一个exe方法: using System; using System.Collections.Generic; using System.Runtime.InteropServices; us ...

  8. wpf只运行一个实例

    原文:wpf只运行一个实例 在winform下,只运行一个实例只需这样就可以: 1. 首先要添加如下的namespace: using System.Threading; 2. 修改系统Main函数, ...

  9. Winform(C#)限制程序只运行一个实例

    C#控制只运行开启一个程序 在这个例子中不需要调用ReleaseMutex,mutex会在程序结束时自动释放.为了防止mutex过早释放,在程序的最后调用下GC.KeepAlive (mutex). ...

随机推荐

  1. POJ 2516 Minimum Cost(最小费用流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  2. HDU 4735 Little Wish~ lyrical step~(DLX搜索)(2013 ACM/ICPC Asia Regional Chengdu Online)

    Description N children are living in a tree with exactly N nodes, on each node there lies either a b ...

  3. truffle开发一个简单的Dapp

    1.安装Truffle:npm install -g truffle 2.建立项目目录并进入:mkdir pet-shop-tutorial cd pet-shop-tutorial 3.使用truf ...

  4. POI 导入 一直报400问题

    排查过程:1.400一般都是参数或者请求不对,但是我这个情况是本地好用,只是服务器有问题,所以排除了传值的格式等问题. 2.服务器和本地网络隔离,所以没办法比较代码,分两次全量覆盖了html和js部分 ...

  5. 算法(6)3Sum Closest

    kSum问题是一类问题,基本的方法是两个循环,其他一个查找,但是今天碰到了一个超级棘手的问题,是3Sum问题的一个变型 问题:给定一个数组,给定一个整数k,要求找出在数组中找到3个整数,使得这三个整数 ...

  6. request.getParameterMap() 获取表单提交的键值对 并且 也能获取动态表单的key

    Map<String,String[]> map = request.getParameterMap();Set<String> keys = map.keySet(); 获取 ...

  7. 洛谷 P1251 餐巾计划问题

    题目链接 最小费用最大流. 每天拆成两个点,早上和晚上: 晚上可以获得\(r_i\)条脏毛巾,从源点连一条容量为\(r_i\),费用为0的边. 早上要供应\(r_i\)条毛巾,连向汇点一条容量为\(r ...

  8. angular js自定义service的简单示例

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. spring aop与aspectj

    AOP:面向切面编程 简介 AOP解决的问题:将核心业务代码与外围业务(日志记录.权限校验.异常处理.事务控制)代码分离出来,提高模块化,降低代码耦合度,使职责更单一. AOP应用场景: 日志记录.权 ...

  10. MUI -- plus初始化原理及plus is not defined,mui is not defined 错误汇总

    不要在没有plus和mui的环境下调用相关API 普通浏览器里没有plus环境,只有HBuilder真机运行和打包后才能运行plus api. 在普通浏览器里运行时plus api时控制台必然会输出p ...