WPF 防止程序多次打开运行

    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private static System.Threading.Mutex mutex;
        protected override void OnStartup(StartupEventArgs e)
        {
            mutex = new System.Threading.Mutex(true, "OnlyRun_CRNS");
            if (mutex.WaitOne(0, false))
            {
                base.OnStartup(e);
            }
            else
            {
                MessageBox.Show("程序已经在运行!", "提示");
                this.Shutdown();
            }
        }
    }
在App.xaml.cs中重写OnStartup实现,OnStartup可以做很多事情,例如:程序初始化、数据库初始化、命令行参数接收等。

猜你喜欢

转载自blog.csdn.net/lwwl12/article/details/78986883
WPF