只运行一个exe应用程序的使用案例
应用程序的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应用程序的使用案例的更多相关文章
- vc++高级班之窗口篇[4]---让程序只运行一个实例
大家都看过或者使用过类似只运行一个实例的程序,比如:QQ游戏.部分浏览器 等等! 让一个程序只运行一个实例的方法有多种,但是原理都类似,也就是在程序创建后,有窗口的程序在窗口创建前, 检查系统中是 ...
- Linux编程之《只运行一个实例》
概述 有些时候,我们要求一个程序在系统中只能启动一个实例.比如,Windows自带的播放软件Windows Medea Player在Windows里就只能启动一个实例.原因很简单,如果同时启动几个实 ...
- VC只运行一个程序实例
方法有很多,以下只是提供一种用的多的 一. 单文档程序 在程序App类的InitInstance中添加如下代码 BOOL CDDZApp::InitInstance() { /*只运行一个实例*/ / ...
- C# JabLib系列之如何保证只运行一个应用程序的实现
保证只运行一个应用程序的C#实现: using System;using System.Collections.Generic;using System.Linq;using System.Windo ...
- 如何让Windows程序只运行一个程序实例?
要实现VC++或者MFC只运行一个程序实例,一般采用互斥量来实现,即首先用互斥量封装一个只运行一个程序实例的函数接口: HANDLE hMutex = NULL; void MainDlg::RunS ...
- VC 实现程序只运行一个实例,并激活已运行的程序
转载:http://blog.sina.com.cn/s/blog_4b44e1c00100bh69.html 进程的互斥运行:CreateMutex函数实现只运行一个程序实例 正常情况下,一个进程的 ...
- WinFrom 只启动一个exe,并且获得焦点
只启动一个exe方法: using System; using System.Collections.Generic; using System.Runtime.InteropServices; us ...
- wpf只运行一个实例
原文:wpf只运行一个实例 在winform下,只运行一个实例只需这样就可以: 1. 首先要添加如下的namespace: using System.Threading; 2. 修改系统Main函数, ...
- Winform(C#)限制程序只运行一个实例
C#控制只运行开启一个程序 在这个例子中不需要调用ReleaseMutex,mutex会在程序结束时自动释放.为了防止mutex过早释放,在程序的最后调用下GC.KeepAlive (mutex). ...
随机推荐
- VirtualBox上安装ubuntu
当安装完成,重启后,在启动界面出现Please remove the installation medium,then press ENTER.问题? 解决方法:在VirtualBox里面通过iso文 ...
- 重写page的OnInit(学习中总结的)
在写b/s框架的系统的时候,我们会发现,我们经常会在不同的网页中验证Session是否存在,,而我这里没有用Session,用的是MemCache技术,其实它就是键值对. 只不过将Memcache中的 ...
- nodejs安装错误
network错误: npm ERR! network tunneling socket could not be established, cause=connect ECONNREFUSED 12 ...
- FFT多项式乘法模板
有时间来补算法原理orz #include <iostream> #include <cstdio> #include <cmath> #include <c ...
- Powershell使用真实的对象工作
Powershell使用真实的对象工作 来源 https://www.pstips.net/powershell-work-with-reallife-objects.html 每一个Powershe ...
- C++——拷贝构造函数说明
一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: [c-sharp] view plaincopy 1 int a = 100; 2 int b = a; 而类对 ...
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 924 Solved: 351[Submit][Sta ...
- [ST表/贪心] NOI2010 超级钢琴
[NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i ...
- es6+最佳入门实践(9)
9.Iterator和for...of 9.1.Iterator是什么? Iterator又叫做迭代器,它是一种接口,为各种不同的数据结构提供统一的访问机制.这里说的接口可以形象的理解为USB接口,有 ...
- ACM-ICPC 2018 南京赛区网络预赛 Sum
A square-free integer is an integer which is indivisible by any square number except 11. For example ...