C# WinForm 判断程序是否已经在运行,且只允许运行一个实例,附源码
作者:yourber 时间:2021-11-06 14:10:07
我们开发WinFrom程序,很多时候都希望程序只有一个实例在运行,避免运行多个同样的程序,一是没有意义,二是容易出错。
为了更便于使用,笔者整理了一段自己用的代码,可以判断程序是否在运行,只运行一个实例,而且能实现当程序在运行时,再去双击程序图标,直接呼出已经运行的程序。
下面看代码,只需在程序的入口文件中加如下代码即可:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//1.这里判定是否已经有实例在运行
//只运行一个实例
Process instance = RunningInstance();
if (instance == null)
{
//1.1 没有实例在运行
Application.Run(new frmMain());
}
else
{
//1.2 已经有一个实例在运行
HandleRunningInstance(instance);
}
//Application.Run(new frmMain());
}
//2.在进程中查找是否已经有实例在运行
#region 确保程序只运行一个实例
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;
}
//3.已经有了就把它激活,并将其窗口放置最前端
private static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
#endregion
}
标签:WinForm,一个实例
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot大文件上传、分片上传、断点续传、秒传的实现
2023-06-16 02:18:30
Java 格式化输出JSON字符串的2种实现操作
2023-11-13 09:41:10
Java中方法的使用、重载与递归的详细介绍
2022-03-02 02:50:05
![](https://img.aspxhome.com/file/2023/8/60928_0s.png)
intellij idea使用git stash暂存一次提交的操作
2023-03-08 08:30:11
![](https://img.aspxhome.com/file/2023/5/63435_0s.jpg)
通过实例解析Socket套接字通信原理
2023-11-02 20:17:35
![](https://img.aspxhome.com/file/2023/4/58684_0s.png)
浅谈Java多线程编程中Boolean常量的同步问题
2021-06-20 16:36:26
java读取cvs文件并导入数据库
2023-11-25 01:23:13
Java二分查找算法实例详解
2022-07-09 14:33:55
Java overload和override的区别分析
2023-10-12 15:16:17
SpringBoot整合screw实现数据库文档自动生成的示例代码
2023-11-29 05:30:15
![](https://img.aspxhome.com/file/2023/9/60579_0s.png)
Spring Boot集成MyBatis的方法
2021-11-03 23:11:05
![](https://img.aspxhome.com/file/2023/6/64486_0s.png)
SpringBoot 利用thymeleaf自定义错误页面
2023-11-29 08:29:55
![](https://img.aspxhome.com/file/2023/4/60054_0s.png)
SpringBoot自定义注解实现Token校验的方法
2023-11-13 23:17:52
详解Maven profile配置管理及激活profile的几种方式
2022-07-01 08:54:46
java中this与super关键字的使用方法
2022-05-04 22:03:29
一文搞懂MyBatis多数据源Starter实现
2023-07-19 03:34:22
![](https://img.aspxhome.com/file/2023/5/57525_0s.webp)
深入浅析Android消息机制
2023-07-26 03:20:02
![](https://img.aspxhome.com/file/2023/9/85969_0s.png)
SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例
2023-06-29 23:47:34
VS2019配置opencv详细图文教程和测试代码的实现
2023-07-17 13:49:31
![](https://img.aspxhome.com/file/2023/8/100298_0s.png)
Java Swing编写一个简单的计算器软件
2023-08-12 06:24:04
![](https://img.aspxhome.com/file/2023/3/57953_0s.png)