C#只允许启动一个WinFrom进程
[STAThread]
public static void Main()
{
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
if (ret)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现 XP 可视风格
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.Run(new Main());
// Main 为你程序的主窗体,如果是控制台程序不用这句
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 提示信息,可以删除。
Application.Exit();//退出程序
}
}
方法二:禁止多个进程运行,并当重复运行时激活以前的进程
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现 XP 可视风格
System.Windows.Forms.Application.DoEvents();
//There isn't another instance, show our form.
Application.Run(new Main());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
} public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow(instance.MainWindowHandle);
} [DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1;
引用:https://www.cnblogs.com/lidj/archive/2012/07/06/2579897.html
C#只允许启动一个WinFrom进程的更多相关文章
- winfrom程序只启动一个exe进程
private static void KillProcess() { Process process1 = Process.GetCurrentProcess(); //获得当前计算机系统内某个进程 ...
- C# WinForm中如何让当前应用程序只允许启动一个实例
我们在WinForm开发中,很多情况下是需要只允许让用户运行一个实例,那么代码其实很简单.只需要修改Program.cs文件,代码如下 static class Program { /// <s ...
- C#/WPF 仅启动一个进程实例
如何实现仅启动一个 WPF 进程实例,并在打开第二个时,自动唤起之前打开的进程. 1 代码入口 在 App.xaml.cs 文件中,重写 OnStartup 方法,并添加 Mutex 进程锁. /// ...
- WinForm程序,实现只启动一个实例
前言:在我们做的软件中,当点击图标运行时,正常的需求是只需要启动一个软件的实例,这是非常重要的一点,不然就显得我们的软件非常的山寨,笔者在工作中经常遇到同事没有注意这一点,看是不重要,实则非常的重要, ...
- 从内存中加载并启动一个exe
windows似乎只提供了一种启动进程的方法:即必须从一个可执行文件中加载并启动.而下面这段代码就是提供一种可以直接从内存中启动一个exe的变通办法.用途嘛, 也许可以用来保护你的exe,你可以对要保 ...
- flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)
函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...
- C#只启动一个进程的代码
把写内容过程中经常用到的内容做个收藏,如下的内容是关于C#只启动一个进程的内容.public partial class App : Application { protected override ...
- WinFrom 只启动一个exe,并且获得焦点
只启动一个exe方法: using System; using System.Collections.Generic; using System.Runtime.InteropServices; us ...
- 【.net 深呼吸】启动一个进程并实时获取状态信息
地球人和火星人都知道,Process类既可以获取正在运行的进程,也可以启动一个新的进程.在79.77%应用场合,我们只需要让目标进程顺利启动就完事了,至于它执行了啥,有没有出错,啥时候退出就不管了. ...
- init进程 && 解析Android启动脚本init.rc && 修改它使不启动android && init.rc中启动一个sh文件
Android启动后,系统执行的第一个进程是一个名称为init 的可执行程序.提供了以下的功能:设备管理.解析启动脚本.执行基本的功能.启动各种服务.代码的路径:system/core/init,编译 ...
随机推荐
- 【Azure Redis 缓存】Azure Cache for Redis 专用终结点, 虚拟网络, 公网访问链路
问题描述 为优化Redis访问链路,对下面三种方案进行对比: 1.Redis添加到虚拟网络 2.Redis添加专用终结点 3.Redis默认公共链路 问题分析 第三种:Redis默认公共链路,顾名思义 ...
- kotlin协程异常处理之-try catch
kotlin协程小记 协程的async使用 kotlin协程异常处理之-try catch kotlin协程异常处理之-CoroutineExceptionHandler 一.try catch tr ...
- Thinkphp6 自定义命令创建类文件
以创建控制器为例 1.先通过 think 命令创建一个make文件,效果如下图: php think make:command make/MyController 2.修改上面创建的文件[MyCont ...
- RaspberryPi 树莓派 omxplayer 的音量设置方法实测有效!
正确的设置初始音量的方法,其中n/100=你期望的初始dB值, omxplayer --vol n target.media omxplayer介绍 omxplayer 在github中的描述是一个弃 ...
- Android自定义View学习(1)——基础知识介绍
原文:Android自定义View学习(1)--基础知识介绍 - Stars-One的杂货小窝 准备学习自定义View,介绍一下先了解了下相关的前置基础知识,特此总结 本系列集合文章链接可访问Andr ...
- Kotlin学习快速入门(8)—— 委托
原文地址:Kotlin学习快速入门(8)-- 属性委托 - Stars-One的杂货小窝 委托其实是一种设计模式,但Kotlin把此特性编写进了语法中,可以方便开发者快速使用 委托对应的关键字是by ...
- CentOS 安装后必需所做的初始化操作
CentOS 安装后必需所做的初始化操作 #关闭SELinux sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config #关闭防火墙 sy ...
- 记录--用JS轻松实现一个录音、录像、录屏的工具库
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前言 最近项目遇到一个要在网页上录音的需求,在一波搜索后,发现了 react-media-recorder 这个库.今天就跟大家一起研究一 ...
- Swift self, Self, ==, === 傻傻分不清楚?
本文首发于 Ficow Shen's Blog,原文地址: Swift self, Self, ==, === 傻傻分不清楚?. 内容概览 前言 self 和 Self == 和 === 总结 前言 ...
- 【Spring注解驱动开发】@PostConstruct与@PreDestroy源码的执行过程
写在前面 在前面的<[String注解驱动开发]你真的了解@PostConstruct注解和@PreDestroy注解吗?>一文中,我们简单的介绍了@PostConstruct注解与@Pr ...