WPF单例运行(同一个程序只能打开一个)
程序员文章站
2024-03-13 11:58:03
...
1、修改App.xaml.cs
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// 设置程序单例运行
/// </summary>
private static Mutex mutex;
public App()
{
this.Startup += new StartupEventHandler(App_Startup);
}
void App_Startup(object sender, StartupEventArgs e)
{
mutex = new Mutex(true, "MyApp", out bool ret);
if (!ret)
{
MessageBox.Show("程序已经打开");
Environment.Exit(0);
}
}
}
效果:已经打开一个,就不能再打开了(弹框是我自定义的)
原文地址:https://blog.csdn.net/hit_rxz/article/details/40187195