参考
- 豆包
- https://blog.csdn.net/qq_34059233/article/details/145802950
- https://www.cnblogs.com/whuanle/p/12726724.html
环境
| 软件/系统 | 版本 | 说明 |
|---|---|---|
| Windows | windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器 | |
| Microsoft Visual Studio | Community 2022 (64 位) - Current 版本 17.14.17 | |
| .NET Framework | 4.8 |
正文
在 Program.cs 添加如下代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace XiaqiuchuProgram
{internal static class Program{// 唯一标识private const string MutexName = "xiaqiuchu_cnblogs";/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);// 创建互斥体(initialOwner: true 表示当前进程初始拥有该互斥体)using (Mutex mutex = new Mutex(true, MutexName, out bool createdNew)){if (createdNew){// 互斥体是新创建的,说明程序未运行,正常启动Application.Run(new MainFormView());}else{// 互斥体已存在,说明程序已启动,提示用户并激活已有窗口MessageBox.Show("程序已在运行中!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);}}}}
}