[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进程的更多相关文章

  1. winfrom程序只启动一个exe进程

    private static void KillProcess() { Process process1 = Process.GetCurrentProcess(); //获得当前计算机系统内某个进程 ...

  2. C# WinForm中如何让当前应用程序只允许启动一个实例

    我们在WinForm开发中,很多情况下是需要只允许让用户运行一个实例,那么代码其实很简单.只需要修改Program.cs文件,代码如下 static class Program { /// <s ...

  3. C#/WPF 仅启动一个进程实例

    如何实现仅启动一个 WPF 进程实例,并在打开第二个时,自动唤起之前打开的进程. 1 代码入口 在 App.xaml.cs 文件中,重写 OnStartup 方法,并添加 Mutex 进程锁. /// ...

  4. WinForm程序,实现只启动一个实例

    前言:在我们做的软件中,当点击图标运行时,正常的需求是只需要启动一个软件的实例,这是非常重要的一点,不然就显得我们的软件非常的山寨,笔者在工作中经常遇到同事没有注意这一点,看是不重要,实则非常的重要, ...

  5. 从内存中加载并启动一个exe

    windows似乎只提供了一种启动进程的方法:即必须从一个可执行文件中加载并启动.而下面这段代码就是提供一种可以直接从内存中启动一个exe的变通办法.用途嘛, 也许可以用来保护你的exe,你可以对要保 ...

  6. flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)

    函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...

  7. C#只启动一个进程的代码

    把写内容过程中经常用到的内容做个收藏,如下的内容是关于C#只启动一个进程的内容.public partial class App : Application { protected override ...

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

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

  9. 【.net 深呼吸】启动一个进程并实时获取状态信息

    地球人和火星人都知道,Process类既可以获取正在运行的进程,也可以启动一个新的进程.在79.77%应用场合,我们只需要让目标进程顺利启动就完事了,至于它执行了啥,有没有出错,啥时候退出就不管了. ...

  10. init进程 && 解析Android启动脚本init.rc && 修改它使不启动android && init.rc中启动一个sh文件

    Android启动后,系统执行的第一个进程是一个名称为init 的可执行程序.提供了以下的功能:设备管理.解析启动脚本.执行基本的功能.启动各种服务.代码的路径:system/core/init,编译 ...

随机推荐

  1. 通过Glide加载不可见的图片

    今天遇到一个需求,需要点击分享的时候生成图片以及二维码. 即:将带有图片以及二维码的布局文件生成Bitmap,当然这个布局文件是后台生成的,并不可见,这时候会发现使用Glide加载图片没有反应. 源码 ...

  2. C++ //常用查找算法 find_if

    1 //常用查找算法 find_if 2 #include<iostream> 3 #include<string> 4 #include<vector> 5 #i ...

  3. liunx 前台打包的两个报错 Invalid value used in weak set - MIS国产化服务器不支持打包

    错误1 Invalid value used in weak set Webpack4使用 mini-css-extract-plugin 最新版 压缩css 报 "Invalid valu ...

  4. arch安装discover ——KDE商店

    yay  -S discover sudo pacman -S archlinux-appstream-data packagekit-qt5 flatpak fwupd 安装完就可以使用KDE商店了

  5. Git | Git Server 搭建,在自己的服务器上进行 git server 搭建

    系列文章目录 目录 系列文章目录 前言 操作 1. 创建 git 用户 2. 创建 .ssh 目录 3. 自定义仓库的根目录 4. 在服务器上创建个裸仓库 5. 手动配置一个公钥 6. 在本地测试一下 ...

  6. electron程序运行在某些 windows 上白屏

    现象: 打包后的 electron 程序 运行在某些 windows 上白屏 项目情况: vue3.0  项目使用 vue-cli 创建 使用 vue add electron-builder 添加打 ...

  7. day07-JavaScript04

    JavaScript04 11.DOM02 11.3HTML-DOM文档说明 11.3.1基本介绍 在HTML DOM(文档对象模型)中,每个部分都是节点: 1)文档本身是文档节点 2)所有HTML元 ...

  8. .NET周刊【3月第2期 2024-03-17】

    国内文章 开源.NET8.0小项目伪微服务框架(分布式.EFCore.Redis.RabbitMQ.Mysql等) https://www.cnblogs.com/aehyok/p/18058032 ...

  9. 你的DDPG/RDPG为何不收敛?

    园子好多年没有更过了,草长了不少.上次更还是读博之前,这次再更已是博士毕业2年有余,真是令人唏嘘.盗链我博客的人又见长,身边的师弟也问我挖的几个系列坑什么时候添上.这些着实令我欣喜,看来我写的东西也是 ...

  10. 开发必会系列:《spring实战(第4版)》读书笔记

    一  spring的核心 1.主要概念 DI能够让相互协作的软件组件保持松散耦合,而面向切面编程(AOP)允许你把遍布应用各处的功能分离出来形成可重用的组件. spring有两类容器实现方式,bean ...