应用程序单例可以通过下面的几种方法来实现:
1.使用Mutex类
2.使用Semphore类
3.使用EventWaitHandle类
4.继承Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
其中使用Semphore能控制应用程序能够启动的实例的个数,
继承Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase类能够很方便的在下一个实例启动的时候执行相关的代码.
下面分别给出相应的实现代码.例子使用的都是WinForm应用程序.Form1是一个新建的空的窗体.
Demo下载地址:https://files.cnblogs.com/loyldg/SingltonAppDemo.rar
1.使用Mutex

Codeusing System;
using System.Threading;
using System.Windows.Forms;
namespace SingletonAppUsingMutex
{
static class Program
{
/// <summary>
/// 如果名称以前缀"Global\"开头,则Mutex在所有中端服务器会话中均为可见.如果名称以前缀"Local\"开头,则mutex仅在创建它的终端服务器会话中可见(MSDN)
/// </summary>
private static string name = @"Global\singletonAppDemo";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool createdNew;
Mutex mutex = new Mutex(true,name , out createdNew);
if (createdNew)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("Application already running","warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
2.使用Semphore

Codeusing System;
using System.Threading;
using System.Windows.Forms;
namespace SingletonAppUsingSemaphore
{
static class Program
{
private static Semaphore _semaphore;
private static readonly int maxCount = 3;
private static readonly int initCount = 3;
private static string name = "namedSemaphore";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool isNotReachMax;
_semaphore = new Semaphore(initCount, maxCount, name);
isNotReachMax = _semaphore.WaitOne(0, true);
if (isNotReachMax)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show(string.Format("Application already running more than {0} instances", maxCount), "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
3.使用EventWaitHandle

Codeusing System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
namespace SingletonAppUsingEventWaitHandle
{
static class Program
{
private static string name = "SingletonAppUsingEventWaitHandle";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool createdNew;
EventWaitHandle ewh = new EventWaitHandle(true, EventResetMode.ManualReset, name, out createdNew);
if (createdNew)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("Application already running", "warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
4.继承Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
App.cs

Codeusing System.Windows.Forms;
namespace SingletonUsingWindowsFormsApplicationBase
{
public class App:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
public App()
{
this.IsSingleInstance = true;
}
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
{
Application.Run(new Form1());
return false;
}
protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
{
var form = Application.OpenForms[0];
form.WindowState = FormWindowState.Normal;
form.Show();
form.Activate();
}
}
}
Program.cs

Codeusing System;
using System.Windows.Forms;
namespace SingletonUsingWindowsFormsApplicationBase
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
App app = new App();
app.Run(args);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?