当前位置:网站首页>How does Microsoft solve the problem of multiple PC programs
How does Microsoft solve the problem of multiple PC programs
2022-04-23 03:06:00 【Dotnet cross platform】
Preface
Read an article on the official account. 《C# To solve PC The problem of more open end programs 》.
The author created The mutex Mutex Realized :
bool mutexCreated;
var mutex = new Mutex(true, "MyApp", out mutexCreated);
if (mutexCreated)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show(" The program is already open ");
}
Actually ,VB.NET Of WinForm The program provides a simpler implementation :

No code required , Just tick
Nowadays, few people use VB.NET 了 , Can you migrate the same functions to C# What about China? ?
principle
Compile one VB.NET Of WinForm Program , Decompile the source code , Find the entrance Main Method :

This entry class MyApplication Inherited from WindowsFormsApplicationBase Base class , Of the base class actually executed Run Method .
and Run Method used internally IsSingleInstance attribute , Determine whether to enable Single instance application :

and IsSingleInstance Property is assigned in the constructor of the entry class :
public MyApplication()
: base(AuthenticationMode.Windows)
{
base.IsSingleInstance = true;
base.EnableVisualStyles = true;
base.SaveMySettingsOnExit = true;
base.ShutdownStyle = ShutdownMode.AfterMainFormCloses;
}
Realization
I know the principle , We can transform C# The implementation of the :
class Program : WindowsFormsApplicationBase
{
public Program()
{
IsSingleInstance = true;
}
protected override void OnCreateMainForm()
{
MainForm = new Form1();
}
[STAThread]
static void Main(string[] args)
{
new Program().Run(args);
}
}
Need to quote NuGet package Microsoft.VisualBasic
Next , So let's verify that .
Our program can only be opened once , The first 2 This run will activate the running window .
Conclusion
today , We quote Microsoft.VisualBasic Solve the problem of too many programs !
Want to know more about , Please pay attention to my official account ”My IO“
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334318.html
边栏推荐
- 利用正反遍历来解决“字符的最短距离”问题
- Regular object type conversion tool - Common DOM class
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
- Xamarin效果第二十二篇之录音效果
- tf. keras. layers. MaxPooling? D function
- Opencv fills the rectangle with a transparent color
- Dynamic sequence table + OJ
- [software testing] understand the basic knowledge of software testing
- How to count the number of all files in a directory under win10 system
- Array and collection types passed by openfeign parameters
猜你喜欢

荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比

TP5 customization in extend directory succeeded and failed. Return information

AOT和单文件发布对程序性能的影响

Summary of interface automation interview questions for software testing

再战leetcode (290.单词规律)
![[software testing] understand the basic knowledge of software testing](/img/ff/8fcd4b88de28505989aaf517d16113.png)
[software testing] understand the basic knowledge of software testing

Ningde's position in the times is not guaranteed?

Passing object type parameters through openfeign

树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统

REINFORCE
随机推荐
Er and eer models
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
使用栈来解决”迷你语法分析器“的问题
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
Basic SQL (VIII) data update operation practice
Basic workflow of CPU
使用两种方法来解决“最大回文数乘积”问题
【鉴权/授权】自定义一个身份认证Handler
Deep q-network (dqn)
AspNetCore配置多环境log4net配置文件
Plug in for vscode
Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
Close the computer port
C read / write binary file
TP5 customization in extend directory succeeded and failed. Return information
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
最通俗易懂的依赖注入之服务容器与作用域