C# wpf创建单实例应用程序(8)

版权声明:随便看,喜欢的话加我qq,一起讨论。 https://blog.csdn.net/qq_43687284/article/details/84969959

1,新建一个wpf项目,找到App.xmal.cs打开后插入如下代码:

namespace ElectronicNeedleTherapySystem
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        System.Threading.Mutex mutex;
 
        public App()
        {
            this.Startup += new StartupEventHandler(App_Startup);
        }
 
        void App_Startup(object sender, StartupEventArgs e)
        {
            bool ret;
            mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret);
 
            if (!ret)
            {
                MessageBox.Show("已有一个程序实例运行");
                Environment.Exit(0);
            }
 
        }
    }
}

然后运行生成exe程序,不通过vs测试,直接找到exe文件
在这里插入图片描述
测试成功。

猜你喜欢

转载自blog.csdn.net/qq_43687284/article/details/84969959