1、修改App.xaml.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Threading;
namespace Wpf_singletonRun
{
/// <summary>
/// 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)
{
bool ret;
mutex = new Mutex(true, "MyApp", out ret);
if (!ret)
{
MessageBox.Show("程序已经打开");
Environment.Exit(0);
}
}
}
}
效果:只能打开应用程序一次,再打开弹出提示