当前位置:网站首页>微软是如何解决 PC 端程序多开问题的
微软是如何解决 PC 端程序多开问题的
2022-04-23 03:02:00 【dotNET跨平台】
前言
在公众号上看到一篇文章《C#中解决PC端程序多开的问题》。
该文作者是通过创建互斥锁Mutex实现的:
bool mutexCreated;
var mutex = new Mutex(true, "MyApp", out mutexCreated);
if (mutexCreated)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("该程序已打开");
}
其实,VB.NET 的 WinForm 程序提供了一个更简单的实现方式:

无需任何代码,只需打个勾
现在很少有人用 VB.NET 了,能不能迁移相同功能到 C# 中呢?
原理
编译一个 VB.NET 的 WinForm 程序,反编译源代码,找到入口Main方法:

该入口类MyApplication继承自WindowsFormsApplicationBase基类,实际执行的基类的Run方法。
而Run方法内部使用IsSingleInstance属性,判断是否启用单个实例应用程序:

而IsSingleInstance属性是在入口类的构造函数中赋值的:
public MyApplication()
: base(AuthenticationMode.Windows)
{
base.IsSingleInstance = true;
base.EnableVisualStyles = true;
base.SaveMySettingsOnExit = true;
base.ShutdownStyle = ShutdownMode.AfterMainFormCloses;
}
实现
知道了原理,我们就可以改造 C# 的实现:
class Program : WindowsFormsApplicationBase
{
public Program()
{
IsSingleInstance = true;
}
protected override void OnCreateMainForm()
{
MainForm = new Form1();
}
[STAThread]
static void Main(string[] args)
{
new Program().Run(args);
}
}
需要引用 NuGet 包 Microsoft.VisualBasic
接下来,让我们来验证一下。
我们的程序确实只能打开一次,第 2 次运行会将已运行的窗口激活。
结论
今天,我们通过引用Microsoft.VisualBasic解决程序多开的问题!
想了解更多内容,请关注我的个人公众号”My IO“
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124262946
边栏推荐
- How to deploy a website with only a server and no domain name?
- Close the computer port
- Onenet connection process
- Basic SQL (VIII) data update operation practice
- HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
- c#可变参数params的介绍
- AC & A2C & A3C
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
- Reverse a linked list < difficulty coefficient >
- Restart redis
猜你喜欢

Introduction and use of openfeign component
![Niuke white moon race 6 [solution]](/img/c5/6c59378c3bb12efa60ab3a8cd2c943.png)
Niuke white moon race 6 [solution]

AC & A2C & A3C

C# WPF UI框架MahApps切换主题
![[hcip] detailed explanation of six LSAS commonly used by OSPF](/img/31/3b92d42d16a056bf9db9e24471cefd.jpg)
[hcip] detailed explanation of six LSAS commonly used by OSPF

HLS / chisel practice CORDIC high performance computing complex square root

微软是如何解决 PC 端程序多开问题的——内部实现

最通俗易懂的依赖注入与控制反转
![Introduction to ACM [TSP problem]](/img/9f/4e3592542d989b2fbb6d82f7f2fbd2.png)
Introduction to ACM [TSP problem]
![FileNotFoundError: [Errno 2] No such file or directory](/img/ea/0c3f2768d14c1f4bb42bd1309ab996.png)
FileNotFoundError: [Errno 2] No such file or directory
随机推荐
基于多态的职工管理系统源码与一些理解
Android high-level interview must ask: overall business and project architecture design and reconstruction
AspNetCore配置多环境log4net配置文件
Encapsulate components such as pull-down menu based on ele
First knowledge of C language ~ branch statements
Chapter V project quality management of information system project manager summary
The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
Er and eer models
eventBus
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
Openfeign details show
Chapter VI project information management system summary
Classification of technology selection (2022)
[Euler plan question 13] sum of large numbers
Table space capacity query and expansion of Oracle Database
ASP.NET 6 中间件系列 - 条件中间件
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
《信息系统项目管理师总结》第七章 项目沟通管理