WPF只允许一个程序运行最简单方式(单例)

WPF只允许一个程序运行最简单方式(单例)

利用 System.Threading.Mutex来实现控制程序的单例运行

在APP的Startup事件处理程序中

1
2
3
4
5
6
7
8
9
10
11
12
13
public partial class App : Application
{
private static Mutex mutex;
private void Application_Startup(object sender, StartupEventArgs e)
{
mutex = new Mutex(true,"唯一标识",out var createdNew);
if (!createdNew)
{
MessageBox.Show("程序已经在运行中","提示",MessageBoxButton.OK,MessageBoxImage.Error);
Environment.Exit(0);
}
}
}

WPF只允许一个程序运行最简单方式(单例)

https://bubuweiying.site/WPF只允许一个程序运行最简单方式(单例)/

作者

步步为营

发布于

2024-07-11

更新于

2025-03-15

许可协议